package com.adams.custom.page.mgt.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections.map.LinkedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.adams.access.db.bean.ActionType;
import com.adams.access.db.bean.DicColumn;
import com.adams.access.db.bean.DicTable;
import com.adams.access.db.bean.Role;
import com.adams.access.db.bean.User;
import com.adams.access.db.bean.ViewEvent;
import com.adams.access.db.bean.ViewItem;
import com.adams.access.db.bean.ViewPanel;
import com.adams.access.db.bean.ViewWin;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.BusinessDealFlowContext;
import com.adams.custom.page.core.dto.DicColumnDto;
import com.adams.custom.page.core.service.BusinessDealFlowEngineService;
import com.adams.custom.page.core.service.DicTableService;
import com.adams.custom.page.core.service.SQLConstructServie;
import com.adams.custom.page.mgt.ViewTreeConfigDTO;
import com.adams.custom.page.mgt.dao.impl.ViewEventDaoImpl;
import com.adams.custom.page.mgt.dao.impl.ViewItemDaoImpl;
import com.adams.custom.page.mgt.dao.impl.ViewPanelDaoImpl;
import com.adams.custom.page.mgt.dao.impl.ViewWinDaoImpl;
import com.adams.custom.page.mgt.service.ViewParseService;
import com.adams.custom.page.util.ViewCacheUtil;
import com.adams.custom.page.util.ViewConstant;
import com.adams.custom.page.util.ViewUtil;
import com.adams.role.service.RoleService;
import com.adams.user.service.UserService;

/**
 * 
 * File Name : ViewServiceImpl.java
 * 
 * @Description : 界面服务类
 */
public class ViewParseServiceImpl implements ViewParseService {
	Logger logger = Logger.getLogger(ViewParseServiceImpl.class.getName());

	private static String cacheMapByMethodNameAndparameter = "ViewParseServiceImpl_cacheMapByPk";

	private static String CACHE_SEQ = "|";

	/**
	 * Description : 页面窗体数据访问实现类 Author : 904370
	 */
	private ViewWinDaoImpl viewWinDaoImpl;

	/**
	 * Description : 页面面板实体数据访问实现类 Author : 904370
	 */
	private ViewPanelDaoImpl viewPanelDaoImpl;

	/**
	 * Description : 页面信息项实体数据访问实现类 Author : 904370
	 */
	private ViewItemDaoImpl viewItemDaoImpl;
	/**
	 * Description : 页面面板表格按钮访问实现类 Author : 904202
	 */
	private ViewEventDaoImpl viewEventDaoImpl;

	/**
	 * Description : 表定义服务类 Author : 904370
	 */
	private DicTableService dicTableService;

	/**
	 * Description : 业务处理流调用引擎服务类 Author : 904370
	 */
	private BusinessDealFlowEngineService businessDealFlowEngineService;

	/**
	 * Description : sql组装服务类 Author : 904370
	 */
	private SQLConstructServie SQLConstructServie;

	private RoleService roleService;
	
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * Description : 界面面板自定义过滤条件实体数据访问实现类 Author : 904202
	 */
	// private ViewPanelFilterDaoImpl viewPanelFilterDaoImpl;
	//
	// public void setViewPanelFilterDaoImpl(
	// ViewPanelFilterDaoImpl viewPanelFilterDaoImpl)
	// {
	// this.viewPanelFilterDaoImpl = viewPanelFilterDaoImpl;
	// }
	/**
	 * @param viewWinDaoImpl
	 *            the viewWinDaoImpl to set
	 */
	public void setViewWinDaoImpl(ViewWinDaoImpl viewWinDaoImpl) {
		this.viewWinDaoImpl = viewWinDaoImpl;
	}

	/**
	 * @param viewPanelDaoImpl
	 *            the viewPanelDaoImpl to set
	 */
	public void setViewPanelDaoImpl(ViewPanelDaoImpl viewPanelDaoImpl) {
		this.viewPanelDaoImpl = viewPanelDaoImpl;
	}

	/**
	 * @param viewItemDaoImpl
	 *            the viewItemDaoImpl to set
	 */
	public void setViewItemDaoImpl(ViewItemDaoImpl viewItemDaoImpl) {
		this.viewItemDaoImpl = viewItemDaoImpl;
	}

	/**
	 * 
	 * @param viewEventDaoImpl
	 * 
	 */
	public void setViewEventDaoImpl(ViewEventDaoImpl viewEventDaoImpl) {
		this.viewEventDaoImpl = viewEventDaoImpl;
	}

	public void setDicTableService(DicTableService dicTableService) {
		this.dicTableService = dicTableService;
	}

	public SQLConstructServie getSQLConstructServie() {
		return SQLConstructServie;
	}

	public void setSQLConstructServie(SQLConstructServie constructServie) {
		SQLConstructServie = constructServie;
	}

	public void setBusinessDealFlowEngineService(
			BusinessDealFlowEngineService businessDealFlowEngineService) {
		this.businessDealFlowEngineService = businessDealFlowEngineService;
	}

	/**
	 * Description : 根据菜单id获得界面显示窗口布局信息JSON串
	 * 
	 * @param userid
	 * @param menuid
	 * @return
	 * @throws BusinessBusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseViewJSONByMenuId(long userid, long menuid)
			throws BusinessException {
		logger.info("getParseViewJSONByMenuId method run begin.");
		String key = "getParseViewJSONByMenuId" + CACHE_SEQ + menuid;
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		String result = "";
		if (!cacheMap.containsKey(key)) {
			Map map = this.getParseViewByMenuIdInCache(userid, menuid);
			result = JSONObject.fromObject(map).toString();
			cacheMap.put(key, result);
			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			result = (String) cacheMap.get(key);
		}
		logger.info("getParseViewJSONByMenuId method run end.");
		return result;
	}

	/**
	 * Description : 根据菜单id获得界面显示窗口布局信息
	 * 
	 * @param userid
	 * @param menuid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseViewByMenuId(long userid, long menuid)
			throws BusinessException {
		logger.info("getParseViewByMenuId method run begin.");
		Map srcmap = getParseViewByMenuIdInCache(userid, menuid);
		Map result = (Map) ViewUtil.cloneBeanByStream(srcmap);
		logger.info("getParseViewByMenuId method run end.");
		return result;
	}

	@SuppressWarnings("unchecked")
	private Map getParseViewByMenuIdInCache(long userid, long menuid)
			throws BusinessException {
		String key = "getParseViewByMenuId" + CACHE_SEQ + menuid ;
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		Map result = null;
		if (!cacheMap.containsKey(key)) {
			List<ViewWin> viewWins = viewWinDaoImpl
					.findViewWinByMenuidAndWintype(menuid, ViewWin.WINTYPE_MENU);
			result = getParseView(userid, viewWins);
			result.put(ViewConstant.JSON_DEAL_SUCESS, true);
			cacheMap.put(key, result);
			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			result = (Map) cacheMap.get(key);
		}
		return result;
	}

	/**
	 * Description : 获得界面显示窗口布局信息
	 * 
	 * @param userid
	 * @param menuid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private Map getParseView(long userid, List<ViewWin> viewWins)
			throws BusinessException {

		Map result = new HashMap();
		List<Map> viewWinsMap = new ArrayList<Map>();
		if (null != viewWins && viewWins.size() > 0) {
			for (ViewWin viewWin : viewWins) {
				Long viewwinid = viewWin.getId();
				Map viewWinMap = ViewWin2Map(viewWin);
				List<ViewPanel> viewPanels = viewPanelDaoImpl
						.findByViewwinid(viewwinid);
				if (null != viewPanels && viewPanels.size() > 0) {
					for (ViewPanel viewPanel : viewPanels) {
						Map viewPanelMap = ViewPanel2Map(viewPanel);
						viewPanelMap.put(ViewConstant.JSON_ID, viewPanel
								.getId()
								+ ViewConstant.JSON_TYPE_VIEWPANEL);

						String url = viewPanel.getUrl();
						if (!ViewUtil.isNuLLStr(url)) {
							url = ViewUtil.addParameterToUrl(url,
									ViewConstant.VIEW_FORM_PANEL_ID,
									(String) viewPanelMap
											.get(ViewConstant.JSON_ID));
						}
						viewPanelMap.put(ViewConstant.JSON_URL, url);
						viewPanelMap.put(ViewConstant.JSON_STORE_URL, viewPanel
								.getStoreUrl());
						viewWinMap.put(viewPanel.getType(), viewPanelMap);
					}
				}
				viewWinsMap.add(viewWinMap);
			}
		}
		result.put(ViewConstant.JSON_WINS, viewWinsMap);
		return result;
	}

	@SuppressWarnings("unchecked")
	private Map ViewWin2Map(ViewWin viewWin) {
		Map viewWinMap = ViewUtil.Object2Map(viewWin);
		viewWinMap.put(ViewConstant.JSON_ID, viewWin.getId()
				+ ViewConstant.JSON_TYPE_VIEWWIN);
		String width = viewWin.getWidth();
		if (ViewUtil.isNum(width)) {
			viewWinMap.put(ViewConstant.JSON_WIDTH, Integer.valueOf(width));
		}
		String height = viewWin.getHeight();
		if (ViewUtil.isNum(height)) {
			viewWinMap.put(ViewConstant.JSON_HEIGHT, Integer.valueOf(height));
		}
		return viewWinMap;
	}

	@SuppressWarnings("unchecked")
	private Map ViewPanel2Map(ViewPanel viewPanel) {
		Map viewPanelMap = ViewUtil.Object2Map(viewPanel);
		String labelWidth = viewPanel.getLabelWidth();
		if (ViewUtil.isNum(labelWidth)) {
			viewPanelMap.put(ViewConstant.JSON_LABEL_WIDTH, Integer
					.valueOf(labelWidth));
		}
		String width = viewPanel.getWidth();
		if (ViewUtil.isNum(width)) {
			viewPanelMap.put(ViewConstant.JSON_WIDTH, Integer.valueOf(width));
		}
		String height = viewPanel.getHeight();
		if (ViewUtil.isNum(height)) {
			viewPanelMap.put(ViewConstant.JSON_HEIGHT, Integer.valueOf(height));
		}
		return viewPanelMap;
	}

	@SuppressWarnings("unchecked")
	private Map ViewItem2Map(String tablecode, ViewItem viewItem)
			throws BusinessException {
		Map itemMap = ViewUtil.Object2Map(viewItem);

		// 增加界面控件id
		Long id = viewItem.getId();
		itemMap.put(ViewConstant.JSON_ID, id + ViewConstant.JSON_TYPE_VIEWITEM);

		// 处理字段名
		itemMap.put(ViewConstant.JSON_NAME,
				getColumnCodeByItemNameToJson(viewItem));

		// 管理url，在url加上此控件id
		String url = viewItem.getUrl();
		if (!ViewUtil.isNuLLStr(url)) {
			url = ViewUtil.addParameterToUrl(url, ViewConstant.VIEW_ITEM_ID,
					(String) itemMap.get(ViewConstant.JSON_ID));
		}
		itemMap.put(ViewConstant.JSON_URL, url);

		// 处理默认值
		String value = viewItem.getValue();
		if (ViewUtil.isNuLLStr(value)) {
			itemMap.remove("value");
		}

		// 处理正刚表达式
		String regexpr = viewItem.getRegexpr();
		if (ViewUtil.isNuLLStr(regexpr)) {
			itemMap.remove("regexpr");
			itemMap.remove("regexprText");
			itemMap.remove("regexprName");
		}

		String xtype = viewItem.getXtype();
		// 下拉选择框并使用静态数据时，查询选择数据
		List<Map> staticData = new ArrayList<Map>();
		String name = viewItem.getName();
		boolean staticFlag = viewItem.isStaticFlag();
		if (staticFlag && !ViewUtil.isNuLLStr(name)) {
			if (ViewItem.XTYPE_1.equals(xtype)
					|| ViewItem.XTYPE_8.equals(xtype)) {
				staticData = getViewItemElementDatas(tablecode, viewItem);
			}
		}
		itemMap.put(ViewConstant.JSON_STATIC_DATA, staticData);

		// 如果是文件上传类型时，增加上传按钮
		if (ViewItem.XTYPE_5.equals(xtype)) {
			String buttonText = viewItem.getBlankText();
			if (ViewUtil.isNuLLStr(buttonText)) {
				itemMap.put(ViewConstant.JSON_ITEM_BUTTON_TEXT,
						ViewConstant.JSON_ITEM_BUTTON_TEXT_DEFAULT);
			}
		} else {
			itemMap.remove(ViewConstant.JSON_ITEM_BUTTON_TEXT);
		}

		itemMap.remove("multilevelByName");
		itemMap.remove("columncodes");
		itemMap.remove("firstColumncode");
		return itemMap;
	}

	/**
	 * Description : 根据面板id获取面板布局信息JSON串 供查询条使用
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewpanelid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseSearchItemsJSON(long userid, long menuid,
			String viewpanelidStr) throws BusinessException {
		logger.info("getParseSearchItemsJSON method run begin.");
		String key = "getParseSearchItemsJSON" + CACHE_SEQ + viewpanelidStr ;
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		String result = "";
		if (!cacheMap.containsKey(key)) {
			Map map = this.getParseSearchItems(userid, menuid, viewpanelidStr,
					null);
			result = JSONObject.fromObject(map).toString();
			cacheMap.put(key, result);
			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			result = (String) cacheMap.get(key);
		}
		logger.info("getParseSearchItemsJSON method run end.");
		return result;
	}

	/**
	 * Description : 根据面板id获取面板布局信息 供查询条使用
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewpanelid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseSearchItems(long userid, long menuid,
			String viewpanelidStr, Map inputMap) throws BusinessException {
		logger.info("getParseSearchItems method run begin.");
		Long viewpanelid = ViewUtil.getViewControlId(viewpanelidStr);
		Map result = new HashMap();
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewpanelid);
		String action = viewPanel.getAction();
		action = ViewUtil.addParameterToUrl(action,
				ViewConstant.VIEW_FORM_PANEL_ID, (String) result
						.get(ViewConstant.JSON_ID));
		result.put(ViewConstant.JSON_ACTION, action);
		Map fieldSetMap = null;
		if (inputMap == null) {
			viewPanel.setLoadUrl(null);
			fieldSetMap = getParseFieldSetByViewpanelid(viewpanelid,
					new HashMap());
		} else {
			fieldSetMap = getParseFieldSetByViewpanelid(viewpanelid, inputMap);
		}

		Long viewsearchi = viewPanel.getId();
		List<ViewItem> viewSearchItems = viewPanelDaoImpl
				.findItems(viewsearchi);
		String tablecode = viewPanel.getTablecode();
		List<ViewItem> viewItems = new ArrayList<ViewItem>();
		for (ViewItem item : viewSearchItems) {
			String xtype = item.getXtype();
			if (ViewItem.XTYPE_4.equals(xtype)) {
				if (item.getColumncodes().size() > 1) {
					logger.error("配置错误,查询项只能为本级！ item name:" + item.getName());
					throw new BusinessException(
							"view.config.searchitem.need.toplevel.exception");
				}
				// item.setFormat(ViewItem.DATE_FORMAT);
				String fieldLabel = item.getFieldLabel();
				if (null != fieldLabel
						&& fieldLabel.lastIndexOf(ViewConstant.JSON_DATE_MSG) == fieldLabel
								.length() - 2) {
					fieldLabel = fieldLabel.substring(0,
							fieldLabel.length() - 2);
				}
				ViewItem befor = (ViewItem) ViewUtil.cloneBean(item);
				befor.setXtype(ViewItem.XTYPE_3);
				befor.setId(ViewUtil.getRandomLongBySystemTime());
				befor.setName(ViewConstant.JSON_DATE_BEFORE + befor.getName());
				befor.setFieldLabel(fieldLabel
						+ ViewConstant.JSON_DATE_BEFORE_MSG);

				ViewItem after = (ViewItem) ViewUtil.cloneBean(item);
				after.setXtype(ViewItem.XTYPE_3);
				after.setId(ViewUtil.getRandomLongBySystemTime() + 1);
				after.setName(ViewConstant.JSON_DATE_AFTER + after.getName());
				after.setFieldLabel(fieldLabel
						+ ViewConstant.JSON_DATE_AFTER_MSG);
				viewItems.add(befor);
				viewItems.add(after);
			} else {
				viewItems.add(item);
			}
		}
		List<Map> itemMaps = getParseItems(tablecode, viewItems);

		fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);

		result.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("getParseSearchItems method run end.");
		return result;
	}

	/**
	 * Description : 获取页面内容项信息
	 * 
	 * @param tablecode
	 * @param viewItems
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getParseItems(String tablecode, List<ViewItem> viewItems)
			throws BusinessException {
		List<Map> itemMaps = new ArrayList<Map>();
		if (null != viewItems && viewItems.size() > 0) {
			for (ViewItem viewItem : viewItems) {
				Map itemMap = ViewItem2Map(tablecode, viewItem);
				itemMaps.add(itemMap);
			}
		}
		return itemMaps;
	}

	/**
	 * Description : 获取页面内容项对应字段数据项数据信息
	 * 
	 * @param tablecode
	 * @param viewItem
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getViewItemElementDatas(String tablecode,
			ViewItem viewItem) throws BusinessException {
		List<Map> staticData = new ArrayList<Map>();

		DicColumn dicColunm = dicTableService.getFinalDicColumn(tablecode,
				viewItem.getColumncodes());

		Set<String> returnColumns = new HashSet<String>();
		String displayField = viewItem.getDisplayField();
		String valueField = viewItem.getValueField();
		String iconClsField = viewItem.getIconClsField();
		if (!ViewUtil.isNuLLStr(displayField)) {
			returnColumns.add(displayField);
		}
		if (!ViewUtil.isNuLLStr(valueField)) {
			returnColumns.add(valueField);
		}
		if (!ViewUtil.isNuLLStr(iconClsField)) {
			returnColumns.add(iconClsField);
		}

		if (null != dicColunm) {
			String sourceElement = dicColunm.getSourceelement();
			if (!ViewUtil.isNuLLStr(sourceElement)) {
				staticData = dicTableService.getElementsByElementCode(
						sourceElement, returnColumns);
			} else {
				String enumeratemap = dicColunm.getEnumeratemap();
				if (!ViewUtil.isNuLLStr(enumeratemap)) {
					staticData = dicColunm.getEnumerate();
					if (ViewUtil.isNuLLStr(displayField)) {
						viewItem.setDisplayField(DicColumn.ENUMERATE_VALUE_TAG);
					}
					if (ViewUtil.isNuLLStr(valueField)) {
						viewItem.setValueField(DicColumn.ENUMERATE_KEY_TAG);
					}
				} else if (DicColumn.DATATYPE_LOGIC.equals(dicColunm
						.getDatatype())) {
					Map<String, String> enumerate = new HashMap<String, String>();
					enumerate.put(DicColumn.ENUMERATE_KEY_TAG, "true");
					enumerate.put(DicColumn.ENUMERATE_VALUE_TAG, "是");
					staticData.add(enumerate);
					enumerate = new HashMap<String, String>();
					enumerate.put(DicColumn.ENUMERATE_KEY_TAG, "false");
					enumerate.put(DicColumn.ENUMERATE_VALUE_TAG, "否");
					staticData.add(enumerate);
					viewItem.setDisplayField(DicColumn.ENUMERATE_VALUE_TAG);
					viewItem.setValueField(DicColumn.ENUMERATE_KEY_TAG);
				}
			}
		}
		return staticData;
	}

	/**
	 * Description : 获取页面表单动态选项信息
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewitemid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseDynamicCombo(long userid, long menuid,
			String viewitemidStr) throws BusinessException {
		logger.info("getParseDynamicCombo method run begin.");
		Map result = new HashMap();
		Long viewitemid = ViewUtil.getViewControlId(viewitemidStr);
		ViewItem viewItem = viewItemDaoImpl.getByCache(viewitemid);
		if (null == viewItem) {
			logger.error("配置出错,页面项不存在! viewItem id:" + viewitemidStr);
			throw new BusinessException("view.config.item.noexists.exception");
		}
		String tablecode = getTablecodeByViewItem(viewitemid);
		List<Map> dataList = getViewItemElementDatas(tablecode, viewItem);
		result = getParseDynamicComboFromList(userid, menuid, dataList);
		logger.info("getParseDynamicCombo method run end.");
		return result;
	}

	/**
	 * Description : 根据页面内容项得到对应的实体表
	 * 
	 * @param viewitemid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	private String getTablecodeByViewItem(Long viewitemid)
			throws BusinessException {

		ViewItem viewItem = viewItemDaoImpl.getByCache(viewitemid);
		if (null == viewItem) {
			logger.error("配置出错,页面项不存在! viewItem id:" + viewitemid);
			throw new BusinessException("view.config.item.noexists.exception");
		}
		Long viewpanelid = viewItem.getViewpanelid();
		if (viewpanelid == 0) {
			logger.error("配置出错,不存在页面面板配置! viewpanel id:" + viewpanelid);
			throw new BusinessException("view.config.panel.noexists.exception");
		}
		String tablecode = "";
		if (viewpanelid > 0) {
			ViewPanel vewPanel = viewPanelDaoImpl.get(viewpanelid);
			if (null == vewPanel) {
				logger.error("配置出错,不存在页面面板配置! viewpanel id:" + viewpanelid);
				throw new BusinessException(
						"view.config.panel.noexists.exception");
			}
			tablecode = vewPanel.getTablecode();
		}
		if (ViewUtil.isNuLLStr(tablecode)) {
			logger.error("页面面板没有配置对应的数据表! viewpanel id:" + viewpanelid);
			throw new BusinessException(
					"view.config.panel.tablecode.noexists.exception");
		}
		return tablecode;
	}

	/**
	 * Description : 解析列表得到动态选项信息
	 * 
	 * @param userid
	 * @param menuid
	 * @param dataList
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseDynamicComboFromList(long userid, long menuid,
			List dataList) throws BusinessException {
		logger.info("getParseDynamicComboFromList method run begin.");
		Map result = new HashMap();
		List<Map> records = new ArrayList<Map>();
		if (null != dataList && dataList.size() > 0) {
			for (Object object : dataList) {
				if (object instanceof Map) {
					records.add((Map) object);
				} else {
					Map record = new HashMap();
					BeanMap beanMap = new BeanMap(object);
					Set keys = beanMap.keySet();
					Iterator keyIterator = keys.iterator();
					while (keyIterator.hasNext()) {
						String propertyName = (String) keyIterator.next();
						Object value = beanMap.get(propertyName);
						record.put(propertyName, value);
					}
					records.add(record);

				}
			}
		}
		Map combo = new HashMap();
		combo.put(ViewConstant.VIEW_DYNAMICCOMBO_COMBO, records);
		result.put(ViewConstant.VIEW_DYNAMICCOMBO_DATA, combo);
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("getParseDynamicComboFromList method run end.");
		return result;
	}

	/**
	 * Description : 根据内容项id解析界面显示
	 * 
	 * @param userid
	 * @param viewtbarid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseViewJSONByItemId(long userid, String viewitemidStr)
			throws BusinessException {
		logger.info("getParseViewJSONByItemId method run begin.");
		String key = "getParseViewJSONByItemId" + CACHE_SEQ + viewitemidStr ;
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		String result = "";
		if (!cacheMap.containsKey(key)) {
			Map map = this.getParseViewByItemId(userid, viewitemidStr);
			result = JSONObject.fromObject(map).toString();
			cacheMap.put(key, result);
			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			result = (String) cacheMap.get(key);
		}
		logger.info("getParseViewJSONByItemId method run end.");
		return result;
	}

	/**
	 * Description : 根据内容项id解析界面显示
	 * 
	 * @param userid
	 * @param viewtbarid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseViewByItemId(long userid, String viewitemidStr)
			throws BusinessException {
		logger.info("getParseViewByItemId method run begin.");
		Long viewitemid = ViewUtil.getViewControlId(viewitemidStr);
		ViewItem viewItem = viewItemDaoImpl.getByCache(viewitemid);
		String xtype = viewItem.getXtype();
		Map result = null;
		if (ViewItem.XTYPE_7.equals(xtype)) {
			Long viewwinid = viewItem.getViewwinid();
			List<ViewWin> viewWins = new ArrayList<ViewWin>();
			ViewWin viewWin = viewWinDaoImpl.get(viewwinid);
			if (null != viewWin) {
				viewWins.add(viewWin);
			}
			result = getParseView(userid, viewWins);
		}
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("getParseViewByItemId method run end.");
		return result;
	}

	/**
	 * Description : 根据panelid获取操作面板详细信息JSON
	 * 
	 * @param userid
	 * @param viewpanelidStr
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseOperatePanelDetailJSONByPanelid(long userid,
			String viewpanelidStr) throws BusinessException {
		logger
				.info("getParseOperatePanelDetailJSONByPanelid method run begin.");
		String key = "getParseOperatePanelDetailJSONByPanelid" + CACHE_SEQ
				+ viewpanelidStr ;
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		String result = "";
		if (!cacheMap.containsKey(key)) {
			Map map = this.getParseOperatePanelDetailByPanelid(userid,
					viewpanelidStr);
			result = JSONObject.fromObject(map).toString();
			cacheMap.put(key, result);
			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			result = (String) cacheMap.get(key);
		}
		logger.info("getParseOperatePanelDetailJSONByPanelid method run end.");
		return result;
	}

	/**
	 * Description : 根据panelid获取操作面板详细信息
	 * 
	 * @param userid
	 * @param viewpanelidStr
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseOperatePanelDetailByPanelid(long userid,
			String viewpanelidStr) throws BusinessException {
		logger.info("getParseOperatePanelDetailByPanelid method run begin.");
		Long viewpanelid = ViewUtil.getViewControlId(viewpanelidStr);
		Map result = new HashMap();
		ViewPanel viewPanel = this.viewPanelDaoImpl.get(viewpanelid);
		ViewWin viewWin = viewWinDaoImpl.get(viewPanel.getViewwinid());
		if (null == viewWin) {
			logger
					.error("配置出错,页面窗口不存在! viewwin id:"
							+ viewPanel.getViewwinid());
			throw new BusinessException("view.config.win.noexists.exception");
		}
		Map winMap = ViewWin2Map(viewWin);
		result.put(ViewConstant.JSON_WIN, winMap);

		result.put(ViewConstant.JSON_ID, viewpanelid
				+ ViewConstant.JSON_TYPE_VIEWPANEL);
		String action = viewPanel.getAction();
		action = ViewUtil.addParameterToUrl(action,
				ViewConstant.VIEW_FORM_PANEL_ID, (String) result
						.get(ViewConstant.JSON_ID));
		result.put(ViewConstant.JSON_ACTION, action);

		Map fieldSetMap = getParseFieldSetByViewpanelid(viewpanelid,
				new HashMap());

		List<ViewItem> viewPanelItems = viewPanelDaoImpl.findItems(viewpanelid);
		String tablecode = viewPanel.getTablecode();
		List<Map> itemMaps = getParseItems(tablecode, viewPanelItems);
		fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
		result.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("getParseOperatePanelDetailByPanelid method run end.");
		return result;
	}

	// 会弹出页面窗口的按钮的actiontype集合
	static Set<String> actiontypeSetByView = new HashSet<String>();
	static {
		actiontypeSetByView.add(ActionType.ACTIONTYPE_GRID);
		actiontypeSetByView.add(ActionType.ACTIONTYPE_CHILDMANAGE);
		actiontypeSetByView.add(ActionType.ACTIONTYPE_ADDBYSELECT);
	}

	/**
	 * Description : 根据事件id解析界面显示
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewtbarid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseViewByEventId(long userid, long menuid,
			String vieweventidStr, Map inputMap) throws BusinessException {
		logger.info("getParseViewByEventId method run begin.");
		Long vieweventid = ViewUtil.getViewControlId(vieweventidStr);
		ViewEvent viewevent = viewEventDaoImpl.get(vieweventid);
		String actionType = viewevent.getActionType();
		Map result = null;
		if (actiontypeSetByView.contains(actionType)) {
			Long viewwinid = viewevent.getViewwinid();
			List<ViewWin> viewWins = new ArrayList<ViewWin>();
			ViewWin viewWin = viewWinDaoImpl.get(viewwinid);
			if (null == viewWin) {
				logger.error("配置出错,页面窗口不存在! viewwin id:" + viewwinid);
				throw new BusinessException(
						"view.config.win.noexists.exception");
			}
			if (null != viewWin) {
				viewWins.add(viewWin);
			}
			result = getParseView(userid, viewWins);
			// 此方法主要用于一对多的页面维护中
			addRelationParamToChildManageView(inputMap, viewevent, result);
			// 此方法主要用于多对多的新增中
			addRelationParamToAddSelectView(inputMap, viewevent, result);
		} else {
			result = getParseFormByEvent(userid, menuid, viewevent, inputMap);
		}
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("getParseViewByEventId method run end.");
		return result;
	}

	/**
	 * Description : 加入关系参数去显示子级页面面板中 提供给 子级维护动作使用 (此方法的目的就是在子级列表里只显示前表所选择记录的子级
	 * 及增加记录按钮时使用) 此方法加入的值,将会在
	 * sqlconstructServiceImpl中的ConstructConditionSQLByGridPanelRelationParam方法中使用
	 * 并且在对象新增的时候也会使用 参数的实体结构为: gridrelationparam={relation:
	 * 'tablecodeB,columncodeC',lastcolumnvalue:'1',range:' 1 ' }
	 * relation为表字段的关系说明, 如上面例子中表tablecodeB的父级字段为columncodeC
	 * lastcolumnvalue为最后字段的数值,为父级字段的主键数值,上例 tablecodeB的columncodeC字段的值为1
	 * range为范围说明,主要是给查询使用,表明是查询此父级下的所有子级还是此父级下可选的子级
	 * 
	 * @param inputMap
	 * @param viewevent
	 * @param result
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void addRelationParamToChildManageView(Map inputMap,
			ViewEvent viewevent, Map result) throws BusinessException {
		String actionType = viewevent.getActionType();
		if (ActionType.ACTIONTYPE_CHILDMANAGE.equals(actionType)) {
			ViewPanel sourceViewPanel = viewPanelDaoImpl.get(viewevent
					.getViewpanelid());
			Map viewWinMap = (Map) ((List) result.get(ViewConstant.JSON_WINS))
					.get(0);
			if (!ViewUtil.isNuLLValueInMap(viewWinMap, ViewPanel.TYPE_GRID)) {
				Map gridPanelMap = (Map) viewWinMap.get(ViewPanel.TYPE_GRID);
				String sourceTablecode = sourceViewPanel.getTablecode();
				addRelationParam(inputMap, gridPanelMap, sourceTablecode);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void addRelationParam(Map inputMap, Map gridPanelMap,
			String sourceTablecode) throws BusinessException {
		String targetTablecode = (String) gridPanelMap
				.get(ViewConstant.JSON_TABLECODE);
		DicTable sourceDicTable = dicTableService.getDicTable(sourceTablecode);
		if (sourceDicTable.isCompositeKey()) {
			logger.error(sourceTablecode + "表为复合主键表,不能做为其它表的来源!");
			throw new BusinessException("table.no.only.keycolumn.exception");
		}

		List<DicColumn> dicColumns = dicTableService
				.findDBDicColumnsByTablecode(targetTablecode);
		int sourcetableshowtime = 0;
		DicColumn parentDicColumn = null;
		for (DicColumn dicColumn : dicColumns) {
			if (sourceTablecode.equals(dicColumn.getSourcetable())) {
				if (sourcetableshowtime > 1) {
					break;
				}
				sourcetableshowtime++;
				parentDicColumn = dicColumn;
			}
		}
		if (sourcetableshowtime == 0) {
			logger
					.error(targetTablecode + "表中没有字段是来源与" + sourceTablecode
							+ "表");
			throw new BusinessException("table.config.exception");
		}
		// 当目标表中有多个字段的值来源与来源表,必须在列表ViewPanel指定一个父级字段
		if (sourcetableshowtime > 1) {
			String parentcolumn = (String) gridPanelMap.get("parentcolumn");
			if (ViewUtil.isNuLLStr(parentcolumn)) {
				logger.error(targetTablecode + "表中有多个字段是来源与" + sourceTablecode
						+ "表,并且显示列表面板中没有指定使用的父级字段!");
				throw new BusinessException("view.config.exception");
			}
			parentDicColumn = dicTableService.getDicColumn(targetTablecode,
					parentcolumn);
		}

		Map<String, String> relateparam = new HashMap<String, String>();

		relateparam.put(ViewConstant.JSON_GRID_RELATION_PARAM_RANGE,
				ViewConstant.JSON_GRID_RELATION_PARAM_RANGE_INCLUDE);
		String relation = parentDicColumn.getDicColumnPK().getTablecode()
				+ ViewConstant.COLUMN_SEQ
				+ parentDicColumn.getDicColumnPK().getColumncode();
		relateparam.put(ViewConstant.JSON_GRID_RELATION_PARAM_RELATION,
				relation);
		String sourcekey = inputMap.get(sourceDicTable.getKeycolumns())
				.toString();
		relateparam.put(ViewConstant.JSON_GRID_RELATION_PARAM_LCV, sourcekey);
		Map param = new HashMap();
		param.put(ViewConstant.JSON_GRID_RELATION_PARAM, relateparam);
		String paramStr = JSONObject.fromObject(param).toString();
		// 写入关系参数
		gridPanelMap.put(ViewConstant.JSON_PARAMS, paramStr);
	}

	/**
	 * Description : 加入关系参数去显示多对多的多对象选择中,为子级页面面板中 提供给 查询动作使用
	 * (此方法的目的就是在子级列表里只显示前表没有的可选择记录的子级 及增加记录按钮时使用) 此方法加入的值,将会在
	 * sqlconstructServiceImpl中的ConstructConditionSQLByGridPanelRelationParam方法中使用
	 * 并且在对象新增的时候也会使用 参数的实体结构为: gridrelationparam={relation:
	 * 'tablecodeA,columncodeA,tablecodeB,columncodeA,tablecodeB,columncodeC',la
	 * s t c o l u m n v a l u e : ' 1 ' , r a n g e : ' 0 ' }
	 * relation为表字段的关系说明,
	 * 如上面例子中表tablecodeA的columncodeA字段与表tablecodeB的columncodeA字段关联,最后字段为父级的字段
	 * lastcolumnvalue为最后字段的数值,为父级字段的主键数值,上例 tablecodeB的columncodeC字段的值为1
	 * range为范围说明,主要是给查询使用,表明是查询此父级下可选的其它子级
	 * 
	 * @param inputMap
	 * @param viewevent
	 * @param result
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void addRelationParamToAddSelectView(Map inputMap,
			ViewEvent viewevent, Map result) throws BusinessException {
		String actionType = viewevent.getActionType();
		if (ActionType.ACTIONTYPE_ADDBYSELECT.equals(actionType)) {
			if (ViewUtil.isNuLLValueInMap(inputMap,
					ViewConstant.JSON_GRID_RELATION_PARAM)) {
				logger.error("关系参数出错,输入的关系参数为空.");
				throw new BusinessException("data.input.exception");
			}
			Map<String, String> relateparam = (Map<String, String>) inputMap
					.get(ViewConstant.JSON_GRID_RELATION_PARAM);
			String relation = relateparam
					.get(ViewConstant.JSON_GRID_RELATION_PARAM_RELATION);
			if (ViewUtil.isNuLLStr(relation)) {
				logger.error("关系参数出错,输入的关系结构为空.");
				throw new BusinessException("data.input.exception");
			}
			String sourcekey = relateparam
					.get(ViewConstant.JSON_GRID_RELATION_PARAM_LCV);
			if (ViewUtil.isNuLLStr(sourcekey)) {
				logger.error("关系参数出错,输入的父级id为空.");
				throw new BusinessException("data.input.exception");
			}
			relateparam.put(ViewConstant.JSON_GRID_RELATION_PARAM_RANGE,
					ViewConstant.JSON_GRID_RELATION_PARAM_RANGE_EXCLUDE);
			ViewPanel sourceViewPanel = viewPanelDaoImpl.get(viewevent
					.getViewpanelid());
			Map viewWinMap = (Map) ((List) result.get(ViewConstant.JSON_WINS))
					.get(0);
			if (!ViewUtil.isNuLLValueInMap(viewWinMap, ViewPanel.TYPE_GRID)) {
				Map gridPanelMap = (Map) viewWinMap.get(ViewPanel.TYPE_GRID);
				String sourceTablecode = sourceViewPanel.getTablecode();

				String targetTablecode = (String) gridPanelMap
						.get(ViewConstant.JSON_TABLECODE);
				DicTable targetDicTable = dicTableService
						.getDicTable(targetTablecode);
				if (targetDicTable.isCompositeKey()) {
					logger.error(targetTablecode + "表为复合主键表,不能做为其它表的来源!");
					throw new BusinessException(
							"table.no.only.keycolumn.exception");
				}

				List<DicColumn> dicColumns = dicTableService
						.findDBDicColumnsByTablecode(sourceTablecode);
				int sourcetableshowtime = 0;
				DicColumn sourceDicColumn = null;
				for (DicColumn dicColumn : dicColumns) {
					if (targetTablecode.equals(dicColumn.getSourcetable())) {
						sourcetableshowtime++;
						sourceDicColumn = dicColumn;
					}
				}
				if (sourcetableshowtime == 0) {
					logger.error(sourceTablecode + "表中没有字段是来源与"
							+ targetTablecode + "表");
					throw new BusinessException("table.config.exception");
				}
				// 当来源表中有多个字段的值来源与目标表,不能使用多对多的情况
				if (sourcetableshowtime > 1) {
					boolean findDicColumnflag = false;
					String parentColumn = sourceViewPanel.getParentcolumn();
					if (!ViewUtil.isNuLLStr(parentColumn)) {
						// 当来源表中有两个字段的值来源与目标表时，取非父级关联的字段
						if (sourcetableshowtime == 2) {
							for (DicColumn dicColumn : dicColumns) {
								if (targetTablecode.equals(dicColumn
										.getSourcetable())
										&& !parentColumn.equals(dicColumn
												.getDicColumnPK()
												.getColumncode())) {
									sourceDicColumn = dicColumn;
									findDicColumnflag = true;
									break;

								}
							}
						}
						if (!findDicColumnflag) {
							logger.error(sourceTablecode + "表中有多个字段是来源与"
									+ targetTablecode + "表,无法确定将选择的记录id写入"
									+ sourceTablecode + "表的关联字段中!");
							throw new BusinessException("view.config.exception");
						}
					} else {
						logger.error(sourceTablecode + "表中有多个字段是来源与"
								+ targetTablecode + "表,并且显示列表面板中没有指定使用的父级字段!");
						throw new BusinessException("view.config.exception");
					}
				}

				StringBuilder relationSB = new StringBuilder();
				relationSB.append(targetTablecode);
				relationSB.append(ViewConstant.COLUMN_SEQ);
				relationSB.append(targetDicTable.getKeycolumns());
				relationSB.append(ViewConstant.COLUMN_SEQ);
				relationSB.append(sourceDicColumn.getDicColumnPK()
						.getTablecode());
				relationSB.append(ViewConstant.COLUMN_SEQ);
				relationSB.append(sourceDicColumn.getDicColumnPK()
						.getColumncode());
				relationSB.append(ViewConstant.COLUMN_SEQ);
				relationSB.append(relation);
				relateparam.put(ViewConstant.JSON_GRID_RELATION_PARAM_RELATION,
						relationSB.toString());
				Map param = new HashMap();
				param.put(ViewConstant.JSON_GRID_RELATION_PARAM, relateparam);
				String paramStr = JSONObject.fromObject(param).toString();
				// 写入关系参数
				gridPanelMap.put(ViewConstant.JSON_PARAMS, paramStr);

				// 在action url中加入页面面板id 用于新增操作的解析中
				String action = (String) gridPanelMap
						.get(ViewConstant.JSON_ACTION);
				if (!ViewUtil.isNuLLStr(action)) {
					action = ViewUtil.addParameterToUrl(action,
							ViewConstant.VIEW_FORM_PANEL_ID,
							(String) gridPanelMap.get(ViewConstant.JSON_ID));
				}
				gridPanelMap.put(ViewConstant.JSON_ACTION, action);
			}
		}
	}

	/**
	 * 
	 * Description : 根据 menuid 获取页面表单信息
	 * 
	 * @param userid
	 * @param menuid
	 * @param inputMap
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseFormByMenuId(long userid, long menuid, Map inputMap)
			throws BusinessException {
		logger.info("getParseFormByMenuId method run begin.");

		String result = null;
		Map map = new HashMap();

		List<ViewWin> viewWins = viewWinDaoImpl.findViewWinByMenuidAndWintype(
				menuid, ViewWin.WINTYPE_MENU);
		if (viewWins == null || viewWins.isEmpty()) {
			logger.error("配置出错,页面窗口不存在! menuid id:" + menuid);
			throw new BusinessException("view.config.win.noexists.exception");
		}

		ViewWin viewWin = viewWins.get(0);
		if (null == viewWin) {
			logger.error("配置出错,页面窗口不存在! menuid id:" + menuid);
			throw new BusinessException("view.config.win.noexists.exception");
		}

		Map winMap = ViewWin2Map(viewWin);
		map.put(ViewConstant.JSON_WIN, winMap);

		List<ViewPanel> viewPanels = viewPanelDaoImpl.findByViewwinid(viewWin
				.getId());
		if (null == viewPanels || viewPanels.size() == 0) {
			logger.error("配置出错,页面面板不存在! viewwin id:" + viewWin.getId());
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		String sourceTablecode = "";
		DicTable sourceDicTable = null;

		int formPanelNum = 0;
		for (ViewPanel viewPanel : viewPanels) {
			if ("formPanel".equals(viewPanel.getType())) {
				sourceTablecode = viewPanel.getTablecode();
				sourceDicTable = dicTableService.getDicTable(sourceTablecode);
				formPanelNum++;
			}
		}

		if (formPanelNum > 1) {
			logger.error("只允许配置一个formPanel！");
			throw new BusinessException("view.config.exception");
		}

		for (ViewPanel viewPanel : viewPanels) {
			Long viewpanelid = viewPanel.getId();
			String tablecode = viewPanel.getTablecode();

			if ("formPanel".equals(viewPanel.getType())) {
				Map fieldSetMap = getParseFieldSetByViewpanelid(viewpanelid,
						inputMap);
				List<ViewItem> viewPanelItems = viewPanelDaoImpl
						.findItems(viewpanelid);
				List<Map> itemMaps = getParseItems(tablecode, viewPanelItems);
				fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
				map.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
			} else if ("gridPanel".equals(viewPanel.getType())) {
				if (sourceDicTable == null) {
					logger
							.error("来源表或主表为空，必须配置formPanel，formPanel与gridPanel之间是主从关系！");
					throw new BusinessException("view.config.exception");
				}
				Map viewPanelMap = ViewPanel2Map(viewPanel);
				addRelationParam(inputMap, viewPanelMap, sourceTablecode);
				viewPanelMap.put(ViewConstant.JSON_ID, viewPanel.getId()
						+ ViewConstant.JSON_TYPE_VIEWPANEL);

				String url = viewPanel.getUrl();
				if (!ViewUtil.isNuLLStr(url)) {
					url = ViewUtil.addParameterToUrl(url,
							ViewConstant.VIEW_FORM_PANEL_ID,
							(String) viewPanelMap.get(ViewConstant.JSON_ID));
				}
				viewPanelMap.put(ViewConstant.JSON_URL, url);
				viewPanelMap.put(ViewConstant.JSON_STORE_URL, viewPanel
						.getStoreUrl());
				map.put(viewPanel.getType(), viewPanelMap);
			}
		}
		map.put(ViewConstant.JSON_DEAL_SUCESS, true);
		result = JSONObject.fromObject(map).toString();
		logger.info("getParseFormByMenuId method run end.");
		return result;
	}

	/**
	 * Description : 获取页面表单信息
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewtbarid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private Map getParseFormByEvent(long userid, long menuid,
			ViewEvent viewevent, Map inputMap) throws BusinessException {
		Map result = new HashMap();

		Long viewwinid = viewevent.getViewwinid();
		ViewWin viewWin = viewWinDaoImpl.get(viewwinid);
		if (null == viewWin) {
			logger.error("配置出错,页面窗口不存在! viewwin id:" + viewwinid);
			throw new BusinessException("view.config.win.noexists.exception");
		}
		Map winMap = ViewWin2Map(viewWin);
		result.put(ViewConstant.JSON_WIN, winMap);

		List<ViewPanel> viewPanels = viewPanelDaoImpl
				.findByViewwinid(viewwinid);
		if (null == viewPanels || viewPanels.size() == 0) {
			logger.error("配置出错,页面面板不存在! viewwin id:" + viewwinid);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		ViewPanel viewPanel = viewPanels.get(0);
		Long viewpanelid = viewPanel.getId();
		result.put(ViewConstant.JSON_ID, viewpanelid
				+ ViewConstant.JSON_TYPE_VIEWPANEL);
		String action = viewPanel.getAction();
		// 在action url中加入页面面板id 用于新增操作的解析中
		action = ViewUtil.addParameterToUrl(action,
				ViewConstant.VIEW_FORM_PANEL_ID, (String) result
						.get(ViewConstant.JSON_ID));
		// 如果有关系参数(表示此页面为在父级的子级维护页面中的增加操作)
		// 把关系参数中的父级id写入action url里面
		String actionType = viewevent.getActionType();
		if (ActionType.ACTIONTYPE_ADD.equals(actionType)) {
			if (!ViewUtil.isNuLLValueInMap(inputMap,
					ViewConstant.JSON_GRID_RELATION_PARAM)) {
				Map<String, String> param = (Map<String, String>) inputMap
						.get(ViewConstant.JSON_GRID_RELATION_PARAM);
				String relation = param
						.get(ViewConstant.JSON_GRID_RELATION_PARAM_RELATION);
				if (ViewUtil.isNuLLStr(relation)) {
					logger.error("关系参数出错,输入的关系结构为空.");
					throw new BusinessException("data.input.exception");
				}
				String sourcekey = param
						.get(ViewConstant.JSON_GRID_RELATION_PARAM_LCV);
				if (ViewUtil.isNuLLStr(sourcekey)) {
					logger.error("关系参数出错,输入的父级id为空.");
					throw new BusinessException("data.input.exception");
				}
				int pos = relation.indexOf(ViewConstant.COLUMN_SEQ);
				int len = relation.length();
				if (pos == -1 || pos + 1 == len) {
					logger.error("关系参数出错,输入的关系结构为:" + relation);
					throw new BusinessException("data.input.exception");
				}
				String columncode = relation.substring(pos + 1, len);
				action = ViewUtil.addParameterToUrl(action, columncode,
						sourcekey);
			}
		}
		result.put(ViewConstant.JSON_ACTION, action);

		Map fieldSetMap = getParseFieldSetByViewpanelid(viewpanelid, inputMap);

		List<ViewItem> viewPanelItems = viewPanelDaoImpl.findItems(viewpanelid);
		String tablecode = viewPanel.getTablecode();
		List<Map> itemMaps = getParseItems(tablecode, viewPanelItems);
		fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
		result.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		return result;
	}

	@SuppressWarnings("unchecked")
	private Map getParseFieldSetByViewpanelid(Long viewpanelid, Map inputMap)
			throws BusinessException {
		Map fieldSetMap = new HashMap();
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewpanelid);
		fieldSetMap.put(ViewConstant.JSON_MAX_COLS, viewPanel.getMaxCols());
		if (ViewUtil.isNum(viewPanel.getLabelWidth())) {
			fieldSetMap.put(ViewConstant.JSON_LABEL_WIDTH, Integer
					.valueOf(viewPanel.getLabelWidth()));
		} else {
			fieldSetMap.put(ViewConstant.JSON_LABEL_WIDTH, viewPanel
					.getLabelWidth());
		}
		String loadUrl = this.buildViewPanelLoadUrl(viewPanel.getId(),
				inputMap, viewpanelid + ViewConstant.JSON_TYPE_VIEWPANEL);
		if (null != loadUrl && !"".equals(loadUrl.trim())) {
			fieldSetMap.put(ViewConstant.JSON_LOAD_URL, loadUrl);
		}
		return fieldSetMap;
	}

	@SuppressWarnings("unchecked")
	private String buildViewPanelLoadUrl(Long viewpanelid, Map inputMap,
			String viewControlId) throws BusinessException {
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewpanelid);
		String tablecode = viewPanel.getTablecode();
		if (ViewUtil.isNuLLStr(tablecode)) {
			logger.error("配置错误,界面面板没有配置对应的表! viewpanel id:" + viewpanelid);
			throw new BusinessException(
					"view.config.panel.tablecode.noexists.exception");
		}
		DicTable dicTable = dicTableService.getDicTable(tablecode);
		String loadUrl = viewPanel.getLoadUrl();
		if (!ViewUtil.isNuLLStr(loadUrl)) {
			if (null == inputMap) {
				throw new BusinessException("data.input.exception");
			}
			// dicTableService.checkDicTableKeycolumns(tablecode, inputMap);
			//
			// List<String> keycolumns = dicTable.getKeycolumnlist();
			Map data = new HashMap();
			List<Map> items = new ArrayList<Map>();
			// Map item = new HashMap();
			// for (String keycolumn : keycolumns)
			// {
			// if (!inputMap.containsKey(keycolumn)
			// && null == inputMap.get(keycolumn))
			// {
			// throw new BusinessException(
			// "data.input.donthaskeycolumn.exception");
			// }
			// String value = inputMap.get(keycolumn).toString();
			// item.put(keycolumn, value);
			// }
			items.add(inputMap);
			data.put(ViewConstant.JSON_ITEMS, items);
			String dataStr = JSONObject.fromObject(data).toString();
			loadUrl = ViewUtil.addParameterToUrl(loadUrl,
					ViewConstant.VIEW_DATA_PARAM, dataStr);
			loadUrl = ViewUtil.addParameterToUrl(loadUrl,
					ViewConstant.VIEW_FORM_PANEL_ID, viewControlId);
		}
		return loadUrl;
	}

	/**
	 * Description : 根据界面输入动态增加数据
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewformid
	 * @param inputMap
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map saveOrUpdateDataInViewForm(long userid, long menuid,
			String viewformidStr, Map inputMap) throws BusinessException {
		logger.info("saveOrUpdateDataInViewForm method run begin.");
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		Long viewformid = ViewUtil.getViewControlId(viewformidStr);
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewformid);
		if (null == viewPanel) {
			logger.error("配置出错,页面面板配置不存在! viewpanel id:" + viewformid);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		String tablecode = null;
		List<Map> inputMaps = new ArrayList<Map>();
		List<ViewEvent> viewEvents = null;
		// 有关联参数 表示为多对多新增
		if (!ViewUtil.isNuLLValueInMap(inputMap,
				ViewConstant.JSON_GRID_RELATION_PARAM)) {
			Map<String, String> param = (Map<String, String>) inputMap
					.get(ViewConstant.JSON_GRID_RELATION_PARAM);
			String relation = param
					.get(ViewConstant.JSON_GRID_RELATION_PARAM_RELATION);
			if (ViewUtil.isNuLLStr(relation)) {
				logger.error("关系参数出错,输入的关系结构为空.");
				throw new BusinessException("data.input.exception");
			}
			String sourcekey = param
					.get(ViewConstant.JSON_GRID_RELATION_PARAM_LCV);
			if (ViewUtil.isNuLLStr(sourcekey)) {
				logger.error("关系参数出错,输入的父级id为空.");
				throw new BusinessException("data.input.exception");
			}
			List<String> tablecodes = new ArrayList<String>();
			List<String> columncodes = new ArrayList<String>();
			StringTokenizer st = new StringTokenizer(relation,
					ViewConstant.COLUMN_SEQ);
			while (st.hasMoreTokens()) {
				String tableorcolumn = st.nextToken();
				if (tablecodes.size() == columncodes.size()) {
					tablecodes.add(tableorcolumn);
				} else {
					columncodes.add(tableorcolumn);
				}
			}
			tablecode = tablecodes.get(tablecodes.size() - 1);

			// 组装新增对象
			String inputTableCode = viewPanel.getTablecode();
			DicTable inputDicTable = dicTableService
					.getDicTable(inputTableCode);
			if (inputDicTable.isCompositeKey()) {
				logger.error(inputTableCode + "表为复合主键表,不能做为其它表的来源!");
				throw new BusinessException("table.no.only.keycolumn.exception");
			}

			// 解析选择的对象(多选)
			List<Map> selectDatas = ViewUtil.getMultiChoiceData(inputMap);
			if (null == selectDatas || selectDatas.size() == 0) {
				return result;
			}
			String selectDataKeyColumn = columncodes.get(0);
			String newDataSelectKeyColumn = columncodes.get(1);
			String newDataParentKeyColumn = columncodes.get(2);
			// 组装新增的中间表数值
			for (Map selectData : selectDatas) {
				Map newData = new HashMap();
				String selectkey = selectData.get(selectDataKeyColumn)
						.toString();
				if (ViewUtil.isNuLLStr(selectkey)) {
					logger.error("选择的记录中没有包含主键值.");
					throw new BusinessException("data.input.exception");
				}
				newData.put(newDataParentKeyColumn, sourcekey);
				newData.put(newDataSelectKeyColumn, selectkey);
				inputMaps.add(newData);
			}

			viewEvents = viewEventDaoImpl.findByViewwinid(viewPanel
					.getViewwinid());

		} else {
			tablecode = viewPanel.getTablecode();
			inputMaps.add(inputMap);

			viewEvents = viewEventDaoImpl.findByViewpanelid(viewformid);
			if (null == viewEvents || viewEvents.size() == 0) {
				viewEvents = viewEventDaoImpl.findByViewwinid(viewPanel
						.getViewwinid());
			}
		}

		if (null == viewEvents || viewEvents.size() == 0) {
			logger.error("配置错误，界面事件不存在! viewpanel id:" + viewformid);
			throw new BusinessException("view.config.event.noexists.exception");
		}
		ViewEvent event = viewEvents.get(0);
		BusinessDealFlowContext context = new BusinessDealFlowContext(
				tablecode, event.getActionType(), inputMaps);
		businessDealFlowEngineService.run(context);

		logger.info("saveOrUpdateDataInViewForm method run end.");
		return result;
	}

	/**
	 * Description : 根据界面列表输入动态更新数据
	 * 
	 * @param userid
	 * @param menuid
	 * @param gridPanelIdStr
	 * @param inputMaps
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map saveOrUpdateDataInGridPanel(long userid, long menuid,
			String viewEventIdStr, List<Map> inputMaps)
			throws BusinessException {
		logger.info("saveOrUpdateDataInGridPanel method run begin.");
		Long id = ViewUtil.getViewControlId(viewEventIdStr);
		ViewEvent event = viewEventDaoImpl.get(id);
		ViewPanel viewPanel = viewPanelDaoImpl.get(event.getViewpanelid());
		if (null == viewPanel) {
			logger.error("配置出错,页面面板配置不存在! viewpanel id:"
					+ event.getViewpanelid());
			throw new BusinessException("view.config.panel.noexists.exception");
		}
		BusinessDealFlowContext context = new BusinessDealFlowContext(viewPanel
				.getTablecode(), event.getActionType(), inputMaps);
		businessDealFlowEngineService.run(context);
		// TODO Auto-generated method stub
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("saveOrUpdateDataInGridPanel method run end.");
		return result;
	}

	/**
	 * 界面输入动态删除数据
	 * 
	 * @param tableCode
	 * @param list
	 */
	@SuppressWarnings("unchecked")
	public Map deleteDataByViewForm(long userId, String tbarId,
			List<Map> mapList) throws BusinessException {
		logger.info("deleteDataByViewForm method run begin.");
		Long id = ViewUtil.getViewControlId(tbarId);
		ViewEvent event = viewEventDaoImpl.get(id);
		ViewPanel grid = viewPanelDaoImpl.get(event.getViewpanelid());

		DicTable table = dicTableService.getDicTable(grid.getTablecode());
		BusinessDealFlowContext context = new BusinessDealFlowContext(table
				.getTablecode(), event.getActionType(), mapList);
		businessDealFlowEngineService.run(context);
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		result.put("msg", "删除成功"); // add by zhangzaiyun
		logger.info("deleteDataByViewForm method run end.");
		return result;
	}

	/**
	 * Description : 根据界面输入动态更新数据
	 * 
	 * @param userid
	 * @param menuid
	 * @param viewformid
	 * @param inputMap
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getDataByViewForm(long userid, long menuid,
			String viewformidStr, Map inputMap) throws BusinessException {
		logger.info("getDataByViewForm method run begin.");
		Map result = new HashMap();
		Long viewformid = ViewUtil.getViewControlId(viewformidStr);
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewformid);
		String tableCode = viewPanel.getTablecode();
		if (null != viewPanel) {
			String tablecode = viewPanel.getTablecode();
			Map data = dicTableService.getDataByKey(tablecode, inputMap);
			List<ViewItem> viewItems = viewPanelDaoImpl.findItems(viewformid);
			for (ViewItem viewItem : viewItems) {
				String xtype = viewItem.getXtype();
				if (ViewItem.XTYPE_7.equals(xtype)) {
					String name = viewItem.getName();
					if (!ViewUtil.isNuLLValueInMap(data, name)) {
						List<String> srcColumncodes = viewItem.getColumncodes();
						List<String> columncodes = null;
						String displayField = viewItem.getDisplayField();
						if (!ViewUtil.isNuLLStr(displayField)) {
							columncodes = new ArrayList<String>();
							columncodes.addAll(srcColumncodes);
							columncodes.add(displayField);
							Object displayFieldValue = dicTableService
									.findValueByTableCodeAndColumncodes(
											tableCode, columncodes, data);
							data.put(name + "_" + displayField,
									displayFieldValue);
						}
					}
				}
			}
			result.put("data", data);
		}
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		logger.info("getDataByViewForm method run end.");
		return result;
	}

	/**
	 * Description : 通过viewItem的Name获取页面DicColumn的列名
	 * 
	 * @param itemString
	 * @return
	 * @throws BusinessException
	 * 
	 */
	private String getColumnCodeByItemNameToJson(ViewItem viewItem) {

		StringBuilder result = new StringBuilder();
		List<String> columncodes = viewItem.getColumncodes();
		if (null != columncodes && columncodes.size() > 0) {
			for (String columncode : columncodes) {
				result.append(columncode);
				result.append("_");
			}
			result.deleteCharAt(result.length() - 1);
		}
		return result.toString();
	}

	/**
	 * Description : 获取数据表格列类型信息
	 * 
	 * @param viewgridid
	 * @param tablecode
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getFieldList(Long viewgridid, String tablecode)
			throws BusinessException {
		List fieldlist = new ArrayList();
		List<ViewItem> viewItems = this.viewPanelDaoImpl.findItems(viewgridid);
		for (ViewItem viewItem : viewItems) {
			Map fieldmap = new HashMap();// 获取列的类型

			List<String> columncodes = viewItem.getColumncodes();
			DicColumn dicColumn = dicTableService.getFinalDicColumn(tablecode,
					columncodes);

			fieldmap.put(ViewConstant.JSON_NAME,
					getColumnCodeByItemNameToJson(viewItem));
			addViewDataTypeByDicColumnDataType(dicColumn, fieldmap);
			fieldlist.add(fieldmap);
		}
		return fieldlist;
	}

	/**
	 * Description : 根据字段定义 得到界面组件的数据类型
	 * 
	 * @param datatype
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void addViewDataTypeByDicColumnDataType(DicColumn dicColumn,
			Map dealMap) {
		String type = "type";
		String dateFormat = "dateFormat";
		String datatype = dicColumn.getDatatype();
		String enumeratemap = dicColumn.getEnumeratemap();
		if (!ViewUtil.isNuLLStr(enumeratemap)) {
			dealMap.put(type, "list");
			return;
		}
		if (DicColumn.DATATYPE_NUM.equals(datatype)) {
			if (dicColumn.getDatadecimals() > 0) {
				dealMap.put(type, "float");
				return;
			} else {
				dealMap.put(type, "int");
				return;
			}
		} else if (DicColumn.DATATYPE_LOGIC.equals(datatype)) {
			dealMap.put(type, "boolean");
			return;
		} else if (DicColumn.DATATYPE_DATE.equals(datatype)) {
			dealMap.put(type, "date");
			dealMap.put(dateFormat, "Y-m-d");
			return;
		} else if (DicColumn.DATATYPE_TIME.equals(datatype)) {
			dealMap.put(type, "date");
			dealMap.put(dateFormat, "Y-m-d H:i:s");
			return;
		}
		dealMap.put(type, "string");
		return;
	}

	/**
	 * Description : 获取数据表格列具体信息
	 * 
	 * @param viewpanelid
	 * @param tablecode
	 * @return
	 * @throws BusinessException
	 * 
	 */
	private List<DicColumnDto> findDicColumnsByPanelId(Long viewpanelid,
			String tablecode) throws BusinessException {
		List<DicColumnDto> dicColumnDtos = new ArrayList<DicColumnDto>();
		List<ViewItem> viewColumns = this.viewPanelDaoImpl
				.findItems(viewpanelid);
		DicColumnDto dicColumnDto = null;
		for (ViewItem viewColumn : viewColumns) {
			String firstColumncode = viewColumn.getFirstColumncode();
			DicColumn dicColumn = dicTableService.getDicColumn(tablecode,
					firstColumncode);
			dicColumnDto = new DicColumnDto(dicColumn);
			dicColumnDtos.add(dicColumnDto);
			if (viewColumn.isMultilevelByName()) {
				List<String> columncodes = viewColumn.getColumncodes();
				// 判断使用到的表是否在同一个数据库下
				// 如果使用到跨数据库的情况下,不加入查询字段列表中
				if (!dicTableService.isSameDatabase(tablecode, columncodes)) {
					continue;
				}
				// 如果不跨数据库的情况下,加入查询字段列表中
				DicColumn finalDicColumn = dicTableService.getFinalDicColumn(
						tablecode, columncodes);
				String attributecode = getAttributecode(columncodes,
						finalDicColumn);
				if (!attributecode.equals(finalDicColumn.getDicColumnPK()
						.getColumncode())) {
					dicColumnDto = new DicColumnDto(attributecode,
							finalDicColumn);
					dicColumnDtos.add(dicColumnDto);
				}
			}
		}
		return dicColumnDtos;
	}

	private String getAttributecode(List<String> columncodes,
			DicColumn finalDicColumn) throws BusinessException {

		String finalColumncode = finalDicColumn.getDicColumnPK()
				.getColumncode();
		int len = columncodes.size();
		int pos = 0;
		for (int i = len - 1; i > 0; i--) {
			String columncode = columncodes.get(i);
			if (columncode.equals(finalColumncode)) {
				pos = i;
				break;
			}
		}
		StringBuilder attributecodeSB = new StringBuilder();
		for (int i = 0; i < pos; i++) {
			String columncode = columncodes.get(i);
			attributecodeSB.append(columncode);
			attributecodeSB.append(ViewConstant.REPLACE_COLUMN_SEQ);
		}
		attributecodeSB.append(finalColumncode);
		return attributecodeSB.toString();
	}

	/**
	 * Description : 获取数据表格按钮列表
	 * 
	 * @param viewgridid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void getParseEventByPanelid(long userid, Map parseViewMap,
			Long viewpanelid) {
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewpanelid);
		ViewWin viewWin = this.viewWinDaoImpl.get(viewPanel.getViewwinid());
		List<Map> viewTbarsList = new ArrayList<Map>();
		List<Map> viewLisentersList = new ArrayList<Map>();
		List<ViewEvent> viewEvents = viewEventDaoImpl
				.findByViewpanelid(viewpanelid);

		User u = userService.queryById(userid);
		Set<ViewEvent> events = new HashSet<ViewEvent>();
		if(u!=null){
			Role role = u.getRole();
			events = role.getEvents();
		}
		List<ViewEvent> rEvents = new ArrayList<ViewEvent>();
		for (ViewEvent ve : events) {
			if (ve.getViewpanelid().longValue() == viewpanelid) {
				rEvents.add(ve);
			}
		}

		// List<ViewEvent> rigthViewEvents = Right
		if (ViewPanel.TYPE_GRID.equals(viewPanel.getType())
				&& ViewWin.WINTYPE_ITEM.equals(viewWin.getWintype())
				&& viewEvents.size() == 0) {
			ViewEvent viewEvent = new ViewEvent();
			viewEvent.setId(ViewUtil.getRandomLongBySystemTime());
			viewEvent.setEventType(ViewEvent.EVENTTYPE_LISENTERS);
			viewEvent.setActionType(ActionType.ACTIONTYPE_ROWDBLCLICK);
			viewEvents.add(viewEvent);
		}

		if (viewEvents.size() > 0) {
			for (ViewEvent viewEvent : viewEvents) {
				Map viewEvebtMap = ViewUtil.Object2Map(viewEvent);
				String action = viewEvent.getAction();
				viewEvebtMap.put(ViewConstant.JSON_ID, viewEvent.getId()
						+ ViewConstant.JSON_TYPE_VIEWEVENT);
				action = ViewUtil.addParameterToUrl(action,
						ViewConstant.VIEW_EVENT_ID, viewEvebtMap.get(
								ViewConstant.JSON_ID).toString());
				viewEvebtMap.put(ViewConstant.JSON_ACTION, action);
				String eventType = viewEvent.getEventType();
				if (ViewEvent.EVENTTYPE_TBAR.equals(eventType)) {
					for (ViewEvent ve : rEvents) {
						if (ve.getId().longValue() == viewEvent.getId()
								.longValue()) {
							viewTbarsList.add(viewEvebtMap);
						}
					}
				} else if (ViewEvent.EVENTTYPE_LISENTERS.equals(eventType)) {
					for (ViewEvent ve : rEvents) {
						if (ve.getId().longValue() == viewEvent.getId()
								.longValue()) {
							viewLisentersList.add(viewEvebtMap);
						}
					}
				}

			}
		}
		if (viewTbarsList.size() > 0) {
			parseViewMap.put(ViewEvent.EVENTTYPE_TBAR, viewTbarsList);
		}
		if (viewLisentersList.size() > 0) {
			parseViewMap.put(ViewEvent.EVENTTYPE_LISENTERS, viewLisentersList);
		}
	}

	/**
	 * Description : 获取数据表格表头格式列表
	 * 
	 * @param viewpanelid
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getParseViewGridItem(Long viewpanelid)
			throws BusinessException {
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewpanelid);
		String tablecode = viewPanel.getTablecode();
		List<ViewItem> viewItems = viewPanelDaoImpl.findItems(viewpanelid);
		List<Map> viewColumnsList = new ArrayList<Map>();
		for (ViewItem viewItem : viewItems) {
			Map viewColumnMap = new LinkedHashMap();
			if (null != viewItem.getHidden() && viewItem.getHidden()) {
				viewColumnMap.put("hidden", true);
			}
			viewColumnMap.put("header", viewItem.getFieldLabel());
			viewColumnMap.put("dataIndex",
					getColumnCodeByItemNameToJson(viewItem));
			viewColumnMap.put("sortable", true);

			DicColumn finalDicColumn = dicTableService.getFinalDicColumn(
					tablecode, viewItem.getColumncodes());
			if (DicColumn.DATATYPE_URL_IMG.equals(finalDicColumn.getDatatype())) {
				viewColumnMap.put("img", true);
			}
			if (!viewItem.isReadOnly()) {
				Map editorMap = ViewItem2Map(viewPanel.getTablecode(), viewItem);
				viewColumnMap.put("editor", editorMap);
			}
			viewColumnMap.put("xtype", "gridcolumn");
			if (DicColumn.DATATYPE_DATE.equals(finalDicColumn.getDatatype())) {
				viewColumnMap.put("xtype", "datecolumn");
				viewColumnMap.put("format", "Y-m-d");
			}
			if (DicColumn.DATATYPE_TIME.equals(finalDicColumn.getDatatype())) {
				viewColumnMap.put("xtype", "datecolumn");
				viewColumnMap.put("format", "Y-m-d H:i:s");
			}

			// 高级查询设置不使用gridfilter的形式,使用弹出页面窗口的形式,所以下面内容注释处理
			// 表格过滤
			// Map filter = new HashMap();
			// addViewDataTypeByDicColumnDataType(finalDicColumn, filter);
			// filter.put("disabled", false);
			// List<Map> enumeratemap = finalDicColumn.getEnumerate();
			// if (enumeratemap.size() > 0)
			// {
			// List<Map> options = new ArrayList<Map>();
			// for (Map enumerate : enumeratemap)
			// {
			// Map option = new HashMap();
			// option
			// .put("id", enumerate
			// .get(DicColumn.ENUMERATE_KEY_TAG));
			// option.put("text", enumerate
			// .get(DicColumn.ENUMERATE_VALUE_TAG));
			// options.add(option);
			// }
			// filter.put("options", options);
			// filter.put("phpMode", true);
			// }
			// viewColumnMap.put("filter", filter);

			viewColumnsList.add(viewColumnMap);
		}
		return viewColumnsList;
	}

	/**
	 * Description : 获取数据表格数据布局信息
	 * 
	 * @param userid
	 * @param viewpanelId
	 * @param dataList
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseGridDatasFromList(long userid, String gpanelIdStr,
			List dataList, int total) throws BusinessException {
		logger.info("getParseGridDatasFromList method run begin.");
		Map viewGridMap = new HashMap();
		Long viewpanelId = ViewUtil.getViewControlId(gpanelIdStr);
		ViewPanel viewpanel = viewPanelDaoImpl.get(viewpanelId);
		viewGridMap.put(ViewConstant.JSON_DEAL_SUCESS, true);
		Map metaMap = new HashMap();
		metaMap.put("totalProperty", "total");
		metaMap.put("root", "records");
		metaMap.put(ViewConstant.JSON_ID, viewpanel.getId()
				+ ViewConstant.JSON_TYPE_VIEWPANEL);

		String tablecode = viewpanel.getTablecode();// 获取表格内容对应的表

		List<Map> newDatasList = new ArrayList<Map>();
		List<ViewItem> viewItemsList = viewItemDaoImpl
				.findItemByViewPanelid(viewpanelId);
		if (null != dataList && dataList.size() > 0) {
			/*
			 * ---和导出excel方法共用 如还原把注释后面添加的那个方法拿掉 并把上面的List<ViewItem>
			 * viewItemsList = viewItemDaoImpl
			 * .findItemByViewPanelid(viewpanelId); 代码去掉------ // 提高性能,方法内缓存 Map
			 * dataMapCache = new HashMap(); Map<Long, DicColumn>
			 * finalDicColumnMapCache = new HashMap<Long, DicColumn>(); for (int
			 * i = 0; i < dataList.size(); i++) { Object object =
			 * dataList.get(i); Map record = new HashMap(); Map beanMap = null;
			 * if (object instanceof Map) { beanMap = (Map) object; } else {
			 * beanMap = new BeanMap(object); }
			 * 
			 * List<ViewItem> viewItemsList = viewItemDaoImpl
			 * .findItemByViewPanelid(viewpanelId);
			 * 
			 * for (ViewItem viewItem : viewItemsList) { String firstColumncode
			 * = viewItem.getFirstColumncode(); if
			 * (viewItem.isMultilevelByName()) { // 要显示的值为关系表的数值
			 * 
			 * // 下面处理分为两种情况， // 1、在查询结果中包含要显示的值，直接转换显示 List<String> columncodes
			 * = viewItem.getColumncodes(); DicColumn finalDicColumn = null; if
			 * (finalDicColumnMapCache .containsKey(viewItem.getId())) {
			 * finalDicColumn = finalDicColumnMapCache .get(viewItem.getId()); }
			 * else { finalDicColumn = dicTableService.getFinalDicColumn(
			 * tablecode, columncodes);
			 * finalDicColumnMapCache.put(viewItem.getId(), finalDicColumn); }
			 * 
			 * String attributecode = getColumnCodeByItemNameToJson(viewItem);
			 * boolean needGetEnumerateValue = false; String lastColumncode =
			 * columncodes.get(columncodes .size() - 1); String finalColumncode
			 * = finalDicColumn .getDicColumnPK().getColumncode(); if
			 * (finalDicColumn.getEnumerate().size() > 0 &&
			 * !lastColumncode.equals(finalColumncode)) { attributecode =
			 * getAttributecode(columncodes, finalDicColumn);
			 * needGetEnumerateValue = true; } String name =
			 * getColumnCodeByItemNameToJson(viewItem); Object value = null;
			 * 
			 * if (beanMap.containsKey(attributecode)) { if (null !=
			 * beanMap.get(attributecode)) { String beanValue =
			 * beanMap.get(attributecode) .toString(); if
			 * (needGetEnumerateValue) { value = finalDicColumn
			 * .getEnumerateValue(beanValue); } else { value = beanValue; } }
			 * else { value = ""; } } else { // 2、不包含时，查询数据库得到结构后显示 String
			 * beanValue = ""; if (null != beanMap.get(firstColumncode)) {
			 * beanValue = beanMap.get(firstColumncode) .toString(); } String
			 * cacheKey = name + "=" + beanValue;
			 * 
			 * if (dataMapCache.containsKey(cacheKey)) { value =
			 * dataMapCache.get(cacheKey); } else { value = dicTableService
			 * .findValueByTableCodeAndColumncodes( tablecode, columncodes,
			 * beanMap); dataMapCache.put(cacheKey, value); } } record.put(name,
			 * value); } else { // 要显示的值为本表的数值 if (null !=
			 * beanMap.get(firstColumncode)) { record.put(firstColumncode,
			 * beanMap.get( firstColumncode).toString()); } else {
			 * record.put(firstColumncode, ""); }
			 * 
			 * } } newDatasList.add(record); }
			 */
			this.parseRecordDatasToList(dataList, viewItemsList, tablecode,
					newDatasList, true);
		}
		viewGridMap.put("records", newDatasList);
		viewGridMap.put("total", total);
		metaMap.put("fields", getFieldList(viewpanelId, tablecode));
		viewGridMap.put("metaData", metaMap);
		logger.info("getParseGridDatasFromList method run end.");
		return viewGridMap;
	}

	/**
	 * Description : 获取数据表格数据布局信息
	 * 
	 * @param userid
	 * @param gpanelId
	 * @param whereSql
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseGridDatas(long userid, String gpanelIdStr,
			String viewsearchidStr, Map inputMap) throws BusinessException {
		return getParseGridDatas(userid, gpanelIdStr, viewsearchidStr,
				inputMap, true);
	}

	/**
	 * Description : 获取数据表格数据布局信息
	 * 
	 * @param userid
	 * @param gpanelId
	 * @param whereSql
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseGridDatas(long userid, String gpanelIdStr,
			String viewsearchidStr, Map inputMap, boolean useCache)
			throws BusinessException {
		Map viewGridMap = new HashMap();

		Long viewpanelId = ViewUtil.getViewControlId(gpanelIdStr);
		if (viewpanelId == 0) {
			logger.error("配置出错,页面面板配置不存在! viewpanel id:" + gpanelIdStr);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		ViewPanel gridPanel = viewPanelDaoImpl.get(viewpanelId);
		String tablecode = gridPanel.getTablecode();// 获取表格内容对应的表

		List<DicColumnDto> dicColumnDtos = findDicColumnsByPanelId(viewpanelId,
				tablecode);

		if (null != dicColumnDtos && dicColumnDtos.size() > 0) {
			StringBuffer countSql = new StringBuffer();
			List<Map> viewGridDatasList = this.getParseDatas(gridPanel,
					viewsearchidStr, dicColumnDtos, inputMap, countSql,
					useCache);

			int totalcount = dicTableService.getCount(tablecode, countSql
					.toString());

			viewGridMap = getParseGridDatasFromList(userid, gpanelIdStr,
					viewGridDatasList, totalcount);
		}
		return viewGridMap;
	}

	/**
	 * Description : 获取数据表格头布局信息JSON串
	 * 
	 * @param userid
	 * @param gpanelId
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseGridsJSON(long userid, String panelIdStr)
			throws BusinessException {
		logger.info("getParseGridsJSON method run begin.");
	//	String key = "getParseGridsJSON" + CACHE_SEQ + panelIdStr ;
	//	Map cacheMap = ViewCacheUtil
	//			.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		String result = "";
	//	if (!cacheMap.containsKey(key)) {
			Map map = this.getParseGridsInCache(userid, panelIdStr);
			result = JSONObject.fromObject(map).toString();
	//		cacheMap.put(key, result);
	//		ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
	//				cacheMap);
	//	} else {
	//		result = (String) cacheMap.get(key);
	//	}
		logger.info("getParseGridsJSON method run end.");
		return result;
	}

	/**
	 * Description : 获取数据表格头布局信息列表
	 * 
	 * @param userid
	 * @param gpanelId
	 * @return
	 * @throws BusinessException
	 * 
	 */
	public Map getParseGridsRightEvents(long userid, String panelIdStr)
			throws BusinessException {
		logger.info("getParseGridsRightEvents method run begin.");
		//String key = "getParseGridsRightEvents" + CACHE_SEQ + panelIdStr
		//		;
		//Map cacheMap = ViewCacheUtil
		//		.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		Map map = null;
		//if (!cacheMap.containsKey(key)) {
			map = this.getParseGridsInCache(userid, panelIdStr);
			// result = JSONObject.fromObject(map).toString();
			//cacheMap.put(key, map);
			//ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
			//		cacheMap);
		//} else {
		//	map = (Map) cacheMap.get(key);
		//}
		logger.info("getParseGridsRightEvents method run end.");
		return map;
	}

	/**
	 * Description : 获取数据表格头布局信息
	 * 
	 * @param userid
	 * @param gpanelId
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public Map getParseGrids(long userid, String panelIdStr)
			throws BusinessException {
		logger.info("getParseGrids method run begin.");
		Map srcmap = getParseGridsInCache(userid, panelIdStr);
		Map result = (Map) ViewUtil.cloneBeanByStream(srcmap);
		logger.info("getParseGrids method run end.");
		return result;
	}

	@SuppressWarnings("unchecked")
	private Map getParseGridsInCache(long userid, String panelIdStr)
			throws BusinessException {
		//String key = "getParseGrids" + CACHE_SEQ + panelIdStr ;
		//Map cacheMap = ViewCacheUtil
		//		.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		Map result = null;
		//if (!cacheMap.containsKey(key)) {
			Long panelId = ViewUtil.getViewControlId(panelIdStr);
			if (panelId == 0) {
				logger.error("配置出错,页面面板配置不存在! viewpanel id:" + panelId);
				throw new BusinessException(
						"view.config.panel.noexists.exception");
			}
			result = new HashMap();
			result.put(ViewConstant.JSON_DEAL_SUCESS, true);
			getParseEventByPanelid(userid, result, panelId);
			result
					.put(ViewConstant.JSON_COLUMNS,
							getParseViewGridItem(panelId));
			//cacheMap.put(key, result);
			//ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
			//		cacheMap);
		//} else {
		//	result = (Map) cacheMap.get(key);
		//}
		return result;
	}

	/**
	 * Description : 解析页面输入查询条信息
	 * 
	 * @param inputMap
	 * @param viewsearchid
	 * @param alias
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getConditionStrByViewsearchid(Map inputMap,
			Long viewsearchid, String alias) throws BusinessException {
		StringBuilder wheresql = new StringBuilder(" 1=1 ");
		ViewPanel viewSearch = viewPanelDaoImpl.get(viewsearchid);
		if (null == viewSearch) {
			return " 1=1 ";
		}
		String tablecode = viewSearch.getTablecode();
		if (ViewUtil.isNuLLStr(tablecode)) {
			return " 1=1 ";
		}
		List<ViewItem> items = viewPanelDaoImpl.findItems(viewsearchid);
		if (null != items && items.size() > 0) {
			for (ViewItem item : items) {
				String name = getColumnCodeByItemNameToJson(item);

				if (null == name || "".equals(name.trim())) {
					continue;
				}

				String xtype = item.getXtype();
				String value = "";
				if (!ViewItem.XTYPE_4.equals(xtype)) {
					if (!inputMap.containsKey(name)) {
						continue;
					}
					if (null == inputMap.get(name)) {
						continue;
					}
					value = inputMap.get(name).toString();
					if ("".equals(value)) {
						continue;
					}
				}

				DicColumn column = dicTableService.getFinalDicColumn(tablecode,
						item.getColumncodes());
				;
				if (null == column) {
					continue;
				}
				String datatype = column.getDatatype();
				if (ViewItem.XTYPE_3.equals(xtype)) {
					wheresql.append("and ");
					wheresql.append(alias).append(".");
					wheresql.append(name).append("=");
					wheresql.append("'").append(value).append("'");
					wheresql.append(" ");
				} else if (ViewItem.XTYPE_4.equals(xtype)) {
					if (null != inputMap.get(ViewConstant.JSON_DATE_BEFORE
							+ name)) {
						value = inputMap.get(
								ViewConstant.JSON_DATE_BEFORE + name)
								.toString();
						if (!"".equals(value)) {
							value = value + " 00:00:00";
							wheresql.append("and ");
							wheresql.append(alias).append(".");
							wheresql.append(name).append(">=");
							wheresql.append("'").append(value).append("'");
							wheresql.append(" ");
						}
					}

					if (null != inputMap.get(ViewConstant.JSON_DATE_AFTER
							+ name)) {
						value = inputMap.get(
								ViewConstant.JSON_DATE_AFTER + name).toString();
						if (!"".equals(value)) {
							value = value + " 23:59:59";
							wheresql.append("and ");
							wheresql.append(alias).append(".");
							wheresql.append(name).append("<=");
							wheresql.append("'").append(value).append("'");
							wheresql.append(" ");
						}
					}
				} else {
					wheresql.append("and ");
					wheresql.append(alias).append(".");
					if (name.indexOf("_") != -1) {
						name = name.substring(0, name.indexOf("_"));
					}
					wheresql.append(name);
					if (DicColumn.DATATYPE_NUM.equals(datatype)
							|| DicColumn.DATATYPE_LOGIC.equals(datatype)) {
						wheresql.append("=");
						wheresql.append(value);
						wheresql.append(" ");
					} else {
						wheresql.append(" like ");
						wheresql.append("'%").append(value).append("%' ");
					}
					wheresql.append(" ");
				}
			}
		}
		return wheresql.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.custom.page.mgt.service.ViewParseService#getParseTreeData
	 * (com.adams.custom.page.mgt.ViewTreeConfigDTO, java.util.List)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getParseTreeData(ViewTreeConfigDTO viewTreeConfigDTO,
			List datas) throws BusinessException {
		logger.info("getParseTreeData method run begin.");
		String codetype = viewTreeConfigDTO.getCodetype();
		List<Map> result = null;
		if (ViewTreeConfigDTO.CODETYPE_LEVELCODE.equals(codetype)) {
			result = this.getParseTreeDataByLevelCode(viewTreeConfigDTO, datas);
		} else {
			result = this.getParseTreeDataByParendid(viewTreeConfigDTO, datas);
		}
		logger.info("getParseTreeData method run end.");
		return result;
	}

	/**
	 * Description : 层码解析数据产生界面树
	 * 
	 * @param viewTreeConfigDTO
	 * @param datas
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getParseTreeDataByLevelCode(
			ViewTreeConfigDTO viewTreeConfigDTO, List datas)
			throws BusinessException {
		List<Map> result = new ArrayList<Map>();
		if (null != datas && datas.size() > 0) {
			String codeField = viewTreeConfigDTO.getCodeField();
			int len = datas.size();
			Map<String, Map> codeMaps = new HashMap<String, Map>();
			for (int i = 0; i < len; i++) {
				Object srcObject = datas.get(i);
				Map objectMap = null;
				if (srcObject instanceof Map) {
					objectMap = (Map) srcObject;
				} else {
					objectMap = ViewUtil.Object2Map(srcObject);
				}

				String code = objectMap.get(codeField).toString();
				Map treeRecord = getTreeRecord(viewTreeConfigDTO, objectMap);
				codeMaps.put(code, treeRecord);
			}

			// 产生子级
			Set<String> codeSet = codeMaps.keySet();
			List<String> codeList = new ArrayList<String>();
			codeList.addAll(codeSet);
			// 根据code排序
			Collections.sort(codeList, new Comparator<String>() {
				@Override
				public int compare(String o1, String o2) {
					return o1.compareTo(o2);
				}
			});

			int codeslen = codeList.size();
			Set<String> noTopCodeSet = new HashSet<String>();
			for (int i = 1; i < codeslen; i++) {
				String code = codeList.get(i);
				for (int j = i - 1; j >= 0; j--) {
					String parentCode = codeList.get(j);
					boolean isParent = (code.indexOf(parentCode) > -1) ? true
							: false;
					if (isParent) {
						Map parentTreeRecord = codeMaps.get(parentCode);
						Map treeRecord = codeMaps.get(code);
						parentTreeRecord
								.put(ViewTreeConfigDTO.TREE_LEAF, false);
						List<Map> children = null;
						if (parentTreeRecord
								.containsKey(ViewTreeConfigDTO.TREE_CHILDREN)) {
							children = (List<Map>) parentTreeRecord
									.get(ViewTreeConfigDTO.TREE_CHILDREN);
						} else {
							children = new ArrayList<Map>();
						}
						children.add(treeRecord);
						parentTreeRecord.put(ViewTreeConfigDTO.TREE_CHILDREN,
								children);
						codeMaps.put(parentCode, parentTreeRecord);
						noTopCodeSet.add(code);
						break;
					}
				}
			}

			// 产生根
			for (String code : noTopCodeSet) {
				codeMaps.remove(code);
			}

			codeSet = codeMaps.keySet();
			for (String code : codeSet) {
				Map treeRecord = codeMaps.get(code);
				result.add(treeRecord);
			}
		}
		// 整体顺序直接按传过来的原顺序排列，不再另行排序，故注释掉以下代码
		// Collections.sort(result, new Comparator<Map>()
		// {
		// @Override
		// public int compare(Map o1, Map o2)
		// {
		// // TODO Auto-generated method stub
		// String id1 = o1.get(ViewTreeConfigDTO.ID_FIELD).toString();
		// String id2 = o2.get(ViewTreeConfigDTO.ID_FIELD).toString();
		// return id1.compareTo(id2);
		// }
		//
		// });
		return result;
	}

	/**
	 * Description : 顺序码解析数据产生界面树
	 * 
	 * @param viewTreeConfigDTO
	 * @param datas
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getParseTreeDataByParendid(
			ViewTreeConfigDTO viewTreeConfigDTO, List datas)
			throws BusinessException {
		List<Map> result = new ArrayList<Map>();
		if (null != datas && datas.size() > 0) {
			String idField = viewTreeConfigDTO.getIdField();
			String parentidField = viewTreeConfigDTO.getParentidField();

			int len = datas.size();
			Map<String, Map> idMap = new HashMap<String, Map>();
			Map<String, List<Map>> parentidMap = new HashMap<String, List<Map>>();
			for (int i = 0; i < len; i++) {
				Object srcObject = datas.get(i);
				Map objectMap = null;
				if (srcObject instanceof Map) {
					objectMap = (Map) srcObject;
				} else {
					objectMap = ViewUtil.Object2Map(srcObject);
				}

				String id = objectMap.get(idField).toString();
				Map treeRecord = getTreeRecord(viewTreeConfigDTO, objectMap);
				idMap.put(id, treeRecord);
				String parentid = "";
				if (null != objectMap.get(parentidField)) {
					parentid = objectMap.get(parentidField).toString();
				}
				List<Map> parentids = null;
				if (parentidMap.containsKey(parentid)) {
					parentids = parentidMap.get(parentid);
				} else {
					parentids = new ArrayList<Map>();
				}
				parentids.add(treeRecord);
				parentidMap.put(parentid, parentids);
				idMap.put(id, treeRecord);

			}

			// 产生子级
			Set<String> idSet = idMap.keySet();
			for (String id : idSet) {
				if (parentidMap.containsKey(id)) {
					Map treeRecord = idMap.get(id);
					treeRecord.put(ViewTreeConfigDTO.TREE_LEAF, false);
					treeRecord.put(ViewTreeConfigDTO.TREE_CHILDREN, parentidMap
							.get(id));
					idMap.put(id, treeRecord);
				}
			}

			// 产生根
			Map rootMap = new HashMap();
			Set<String> parentidSet = parentidMap.keySet();
			for (String parentid : parentidSet) {
				if (!idMap.containsKey(parentid)) {
					List<Map> parentids = parentidMap.get(parentid);
					rootMap.put(parentid, parentids);
				}
			}

			idSet = rootMap.keySet();
			for (String id : idSet) {
				List<Map> parentids = (List<Map>) rootMap.get(id);
				result.addAll(parentids);
			}
		}
		// 整体顺序直接按传过来的原顺序排列，不再另行排序，故注释掉以下代码
		// Collections.sort(result, new Comparator<Map>()
		// {
		// @Override
		// public int compare(Map o1, Map o2)
		// {
		// // TODO Auto-generated method stub
		// String id1 = o1.get(ViewTreeConfigDTO.ID_FIELD).toString();
		// String id2 = o2.get(ViewTreeConfigDTO.ID_FIELD).toString();
		// return id1.compareTo(id2);
		// }
		//
		// });
		return result;
	}

	@SuppressWarnings("unchecked")
	private Map getTreeRecord(ViewTreeConfigDTO viewTreeConfigDTO, Map objectMap)
			throws BusinessException {
		String idField = viewTreeConfigDTO.getIdField();
		String displayField = viewTreeConfigDTO.getDisplayField();
		String urlField = viewTreeConfigDTO.getUrlField();
		String iconClsField = viewTreeConfigDTO.getIconClsField();
		String iconField = viewTreeConfigDTO.getIconField();
		String checkedField = viewTreeConfigDTO.getCheckedField();

		Map treeRecord = new HashMap();
		String id = objectMap.get(idField).toString();
		treeRecord.put(ViewTreeConfigDTO.ID_FIELD, id);
		String text = objectMap.get(displayField).toString();
		treeRecord.put(ViewTreeConfigDTO.DISPLAN_FIELD, text);

		if (viewTreeConfigDTO.getChecked()) {
			treeRecord.put(ViewTreeConfigDTO.TREE_CHECKED, false);
			if (!ViewUtil.isNuLLStr(checkedField)) {
				String checked = (String) objectMap.get(checkedField);
				if (!ViewUtil.isBoolean(checked)) {
					treeRecord.put(ViewTreeConfigDTO.TREE_CHECKED, Boolean
							.valueOf(checked));
				}
			}
		}
		if (!ViewUtil.isNuLLStr(urlField) && objectMap.containsKey(urlField)) {
			String url = (String) objectMap.get(urlField);
			if (!ViewUtil.isNuLLStr(url)) {
				treeRecord.put(ViewTreeConfigDTO.URL_FIELD, url);
			}
		}
		if (!ViewUtil.isNuLLStr(iconClsField)
				&& objectMap.containsKey(iconClsField)) {
			String iconCls = (String) objectMap.get(iconClsField);
			if (!ViewUtil.isNuLLStr(iconCls)) {
				treeRecord.put(ViewTreeConfigDTO.ICONCLS_FIELD, iconCls);
			}
		}
		if (!ViewUtil.isNuLLStr(iconField) && objectMap.containsKey(iconField)) {
			String icon = (String) objectMap.get(iconField);
			if (!ViewUtil.isNuLLStr(icon)) {
				treeRecord.put(ViewTreeConfigDTO.ICON_FIELD, icon);
			}
		}
		treeRecord.put(ViewTreeConfigDTO.TREE_LEAF, true);
		return treeRecord;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.adams.custom.page.mgt.service.ViewParseService#
	 * getParseViewPanelFilterInputFormByPanelid(long, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public String getParseViewPanelFilterInputFormJsonByPanelid(long userid,
			String viewpanelidStr) throws BusinessException {
		String key = "getParseViewPanelFilterInputFormByPanelid" + CACHE_SEQ
				+ viewpanelidStr;
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		String result = "";
		if (!cacheMap.containsKey(key)) {
			Map resultMap = getParseViewPanelFilterInputFormByPanelid(userid,
					viewpanelidStr);
			result = JSONObject.fromObject(resultMap).toString();
			cacheMap.put(key, result);
			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			result = (String) cacheMap.get(key);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private Map getParseViewPanelFilterInputFormByPanelid(long userid,
			String viewpanelidStr) throws BusinessException {
		Map result = new HashMap();
		logger.info("input parameter userid=" + userid + " viewpanelidStr="
				+ viewpanelidStr);
		Long viewpanelid = ViewUtil.getViewControlId(viewpanelidStr);
		ViewPanel viewPanel = viewPanelDaoImpl.get(viewpanelid);
		if (null == viewPanel) {
			logger.error("数据不存在！viewpanelid=" + viewpanelid);
			throw new BusinessException("data.noexists.exception");
		}
		String type = viewPanel.getType();
		if (!ViewPanel.TYPE_GRID.equals(type)) {
			logger.error("操作对象不是表格！viewpanelid=" + viewpanelid);
			throw new BusinessException(
					"view.config.panel.not.gridPanel.exception");
		}
		String tablecode = viewPanel.getTablecode();
		if (ViewUtil.isNuLLStr(tablecode)) {
			logger.error("表格的tablecode为空！viewpanelid=" + viewpanelid);
			throw new BusinessException(
					"view.config.panel.tablecode.noexists.exception");
		}
		List<ViewItem> viewItems = new ArrayList<ViewItem>();
		List<ViewItem> viewPanelItems = viewPanelDaoImpl.findItems(viewpanelid);
		for (ViewItem viewPanelItem : viewPanelItems) {
			ViewItem viewItem = (ViewItem) ViewUtil
					.cloneBeanByStream(viewPanelItem);
			viewItem.setHidden(false);
			viewItem.setIconClsField(null);
			viewItem.setReadOnly(false);
			viewItem.setAllowBlank(true);
			viewItem.setBlankText(null);
			viewItem.setRegexpr(null);
			viewItem.setRegexprText(null);
			viewItem.setEditable(true);

			List<String> columncodes = viewPanelItem.getColumncodes();
			DicColumn finalDicColumn = dicTableService.getFinalDicColumn(
					tablecode, columncodes);
			if (null == finalDicColumn) {
				logger.error("viewitem配置出错，name解析不出最终使用的字段！viewitemid="
						+ viewPanelItem.getId());
				throw new BusinessException(
						"view.config.item.finalcolumn.noexists.exception");
			}

			// 当倒数第二字段为枚举时，并且最倒数第一字段为枚举的key或者value时
			// 把倒数第一的字段从viewitem中name中去除
			viewItem.setName(viewItem.getNameNoEnumerateTag());

			String xtype = null;
			String sourceElement = finalDicColumn.getSourceelement();
			String sourceTablecode = finalDicColumn.getSourcetable();
			if (!ViewUtil.isNuLLStr(sourceElement)
					&& !ViewUtil.isNuLLStr(sourceTablecode)) {
				viewItem.setStaticFlag(false);
				viewItem.setUrl("admin/view/cfg!getDynamicCombo.action");

				DicTable sourceTable = dicTableService
						.getDicTable(sourceTablecode);
				// 对来源表进行检查
				if (sourceTable.isCompositeKey()) {
					StringBuilder msg = new StringBuilder();
					msg.append(sourceTablecode);
					msg.append("表为复合主键表，不能作为");
					msg.append(finalDicColumn.getDicColumnPK().getTablecode());
					msg.append("表");
					msg.append(finalDicColumn.getDicColumnPK().getColumncode());
					msg.append("字段的来源表！");
					logger.error(msg.toString());
					throw new BusinessException("table.config.exception");
				}
				// 规则说明
				// 如果本身有配置显示的字段,直接使用显示的字段
				// 如果本身没有配置显示的字段就使用表配置里面的名字字段
				// 如果都没有就报异常,配置错误
				if (ViewUtil.isNuLLStr(viewItem.getDisplayField())) {
					String sourceTableNamecolumn = sourceTable.getNamecolumn();
					if (ViewUtil.isNuLLStr(sourceTableNamecolumn)) {
						logger.error(sourceTable + "表没有配置namecolumn字段！");
						throw new BusinessException(
								"table.config.notexists.namecolumn.exception");
					}
					viewItem.setDisplayField(sourceTableNamecolumn);
				}
				// 值字段直接使用来源表的主键字段
				viewItem.setValueField(sourceTable.getKeycolumns());
				xtype = ViewItem.XTYPE_1;
			} else {
				viewItem.setStaticFlag(true);
				// add by 张在云 904176 解决高级搜索下拉框选项不显示的问题
				viewItem.setDisplayField("value");
				viewItem.setValueField("key");
				// end add
				String enumeratemap = finalDicColumn.getEnumeratemap();
				if (!ViewUtil.isNuLLStr(enumeratemap)) {
					if (finalDicColumn.getEnumerate().size() >= 3) {
						xtype = ViewItem.XTYPE_1;
					} else {
						xtype = ViewItem.XTYPE_8;
					}
				}
			}

			if (ViewUtil.isNuLLStr(xtype)) {
				String datatype = finalDicColumn.getDatatype();
				if (DicColumn.DATATYPE_STR.equals(datatype)) {
					xtype = ViewItem.XTYPE_2;
				} else if (DicColumn.DATATYPE_NUM.equals(datatype)) {
					xtype = ViewItem.XTYPE_9;
				} else if (DicColumn.DATATYPE_DATE.equals(datatype)
						|| DicColumn.DATATYPE_TIME.equals(datatype)) {
					xtype = ViewItem.XTYPE_11;
					viewItem.setDatadecimals(finalDicColumn.getDatadecimals());
				} else if (DicColumn.DATATYPE_LOGIC.equals(datatype)) {
					xtype = ViewItem.XTYPE_8;
				} else {
					continue;
				}
			}
			viewItem.setXtype(xtype);
			viewItems.add(viewItem);
		}

		List<Map> itemMaps = getParseItems(tablecode, viewItems);
		result.put(ViewConstant.JSON_ITEMS, itemMaps);
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.adams.custom.page.mgt.service.ViewParseService#
	 * synchronizationCache()
	 */
	public void synchronizationCache() throws BusinessException {
		logger.info("synchronizationCache method run begin.");
		List<ViewWin> viewWins = viewWinDaoImpl.findAll();
		for (ViewWin viewWin : viewWins) {
			long menuid = viewWin.getMenuid();
			try {
				getParseViewJSONByMenuId(0, menuid);
			} catch (BusinessException e) {
				logger.error("menuid=" + menuid, e);
			}
		}

		List<ViewPanel> viewPanels = viewPanelDaoImpl.findAll();
		for (ViewPanel viewPanel : viewPanels) {
			String viewpanelidStr = viewPanel.getId() + "";
			long viewwinid = viewPanel.getViewwinid();
			ViewWin viewWin = this.viewWinDaoImpl.get(viewwinid);
			if (null != viewWin) {
				if (ViewWin.XTYPE_SEARCH.equals(viewWin.getXtype())) {
					try {
						getParseSearchItemsJSON(0, 0, viewpanelidStr);
					} catch (BusinessException e) {
						logger.error("viewpanelid=" + viewpanelidStr, e);
					}
				} else if (ViewWin.XTYPE_GRID.equals(viewWin.getXtype())) {
					try {
						getParseGridsJSON(0, viewpanelidStr);
					} catch (BusinessException e) {
						logger.error("viewpanelid=" + viewpanelidStr, e);
					}
				}
			}
		}
		logger.info("synchronizationCache method run end.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.custom.page.mgt.service.ViewParseService#clearCache()
	 */
	@SuppressWarnings("unchecked")
	public void clearCache() {
		logger.info("clearCache method run begin.");
		Map cacheMap = ViewCacheUtil
				.getServiceCacheMap(cacheMapByMethodNameAndparameter);
		cacheMap.clear();
		ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
				cacheMap);
		logger.info("clearCache method run end.");
	}

	/**
	 * Description : 获取数据表格数据信息导出Excel
	 * 
	 * @param userid
	 * @param gpanelId
	 * @param viewsearchidStr
	 * @param inputMap
	 * @return 临时Excel输入流
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List getParseExcelDatas(long userid, String eventIdStr,
			String viewsearchidStr, Map inputMap) throws BusinessException {
		return getParseExcelDatas(userid, eventIdStr, viewsearchidStr,
				inputMap, true);
	}

	/**
	 * Description : 获取数据表格数据信息导出Excel
	 * 
	 * @param userid
	 * @param eventIdStr
	 * @param viewsearchidStr
	 * @param inputMap
	 * @param useCache
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List getParseExcelDatas(long userid, String eventIdStr,
			String viewsearchidStr, Map inputMap, boolean useCache)
			throws BusinessException {
		List recordList = null;

		Long viewEventId = ViewUtil.getViewControlId(eventIdStr);

		ViewEvent viewEvent = viewEventDaoImpl.get(viewEventId);

		if (null == viewEvent) {
			logger.error("配置出错,事件配置不存在! viewevent id:" + eventIdStr);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		Long viewWinID = viewEvent.getViewwinid();// 获取winid

		/* 获取winid对应的所有panel */
		List<ViewPanel> viewpanels = viewPanelDaoImpl
				.findByViewwinid(viewWinID);

		ViewPanel gridPanel = null;
		for (ViewPanel panel : viewpanels) {
			if (ViewPanel.TYPE_GRID.equals(panel.getType())) {
				gridPanel = panel;
				break;
			}
		}
		if (null == gridPanel) {
			logger.error("配置出错,页面面板配置不存在! viewpanel id:" + eventIdStr);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		Long viewpanelId = gridPanel.getId();

		String tablecode = gridPanel.getTablecode();// 获取表格内容对应的表

		List<DicColumnDto> dicColumnDtos = findDicColumnsByPanelId(viewpanelId,
				tablecode);

		if (null != dicColumnDtos && dicColumnDtos.size() > 0) {
			StringBuffer countSql = new StringBuffer();
			List<Map> viewGridDatasList = this.getParseDatas(gridPanel,
					viewsearchidStr, dicColumnDtos, inputMap, countSql,
					useCache);

			int totalcount = dicTableService.getCount(tablecode, countSql
					.toString());
			recordList = this.getParseExcelDatasFromList(userid, eventIdStr,
					viewGridDatasList);
		}
		return recordList;
	}

	private List getParseDatas(ViewPanel gridPanel, String viewsearchidStr,
			List<DicColumnDto> dicColumnDtos, Map inputMap,
			StringBuffer countSql, boolean useCache) throws BusinessException {
		String tablecode = gridPanel.getTablecode();// 获取表格内容对应的表
		List<Map> viewGridDatasList = null;

		ViewPanel searchPanel = null;
		Long viewsearchid = ViewUtil.getViewControlId(viewsearchidStr);
		if (viewsearchid != 0) {
			searchPanel = viewPanelDaoImpl.get(viewsearchid);
		} else {
			ViewWin gridWin = viewWinDaoImpl.get(gridPanel.getViewwinid());
			if (ViewWin.WINTYPE_MENU.equals(gridWin.getWintype())) {
				List<ViewWin> viewWins = viewWinDaoImpl.findByMenuid(gridWin
						.getMenuid());
				for (ViewWin viewWin : viewWins) {
					if (ViewWin.XTYPE_SEARCH.equals(viewWin.getXtype())) {
						List<ViewPanel> searchPanels = viewPanelDaoImpl
								.findByViewwinid(viewWin.getId());
						if (null != searchPanels && searchPanels.size() > 0) {
							searchPanel = searchPanels.get(0);
						}
						break;
					}
				}
			} else {
				List<ViewPanel> searchPanels = viewPanelDaoImpl
						.findByViewwinid(gridPanel.getViewwinid());
				for (ViewPanel viewPanel : searchPanels) {
					if (ViewPanel.TYPE_FORM.equals(viewPanel.getType())) {
						searchPanel = viewPanel;
						break;
					}
				}
			}
		}

		String targetSQL = null;
		String countTargetSQL = null;
		String orderbySQL = null;
		if (useCache) {
			// 使用缓存,减少解析代码运行次数,提高性能
			String methodName = "getParseDatas" + CACHE_SEQ;
			int methodNamelen = methodName.length();
			StringBuilder sb = new StringBuilder(methodName);
			sb.append("targetSQL");
			sb.append(CACHE_SEQ);
			sb.append(gridPanel.getId());
			String targetSQLkey = sb.toString();
			sb.delete(methodNamelen, sb.length());
			sb.append("countTargetSQL");
			sb.append(CACHE_SEQ);
			sb.append(tablecode);
			String countTargetSQLkey = sb.toString();
			sb.delete(methodNamelen, sb.length());
			sb.append("orderbySQL");
			sb.append(CACHE_SEQ);
			sb.append(gridPanel.getId());
			String orderbySQLkey = sb.toString();

			Map cacheMap = ViewCacheUtil
					.getServiceCacheMap(cacheMapByMethodNameAndparameter);
			if (!cacheMap.containsKey(targetSQLkey)) {
				targetSQL = SQLConstructServie.ConstructTargetSQL(gridPanel);
				cacheMap.put(targetSQLkey, targetSQL);
			} else {
				targetSQL = (String) cacheMap.get(targetSQLkey);
			}

			if (!cacheMap.containsKey(countTargetSQLkey)) {
				countTargetSQL = SQLConstructServie
						.ConstructCountTargetSQL(tablecode);
				cacheMap.put(countTargetSQLkey, countTargetSQL);
			} else {
				countTargetSQL = (String) cacheMap.get(countTargetSQLkey);
			}

			if (!cacheMap.containsKey(orderbySQLkey)) {
				orderbySQL = SQLConstructServie.ConstructOrderBySQL(gridPanel);
				cacheMap.put(orderbySQLkey, orderbySQL);
			} else {
				orderbySQL = (String) cacheMap.get(orderbySQLkey);
			}

			ViewCacheUtil.put2ServiceCacheMap(cacheMapByMethodNameAndparameter,
					cacheMap);
		} else {
			// 不使用缓存,运行解析代码 得到sql语句
			targetSQL = SQLConstructServie.ConstructTargetSQL(gridPanel);
			countTargetSQL = SQLConstructServie
					.ConstructCountTargetSQL(tablecode);
			orderbySQL = SQLConstructServie.ConstructOrderBySQL(gridPanel);
		}

		String conditionSQL = SQLConstructServie.ConstructConditionSQL(
				searchPanel, gridPanel, inputMap);
		String sql = targetSQL + conditionSQL + orderbySQL;
		countSql.append(countTargetSQL).append(conditionSQL);

		logger.info("getParseDatas method run sql:" + sql);
		logger.info("getParseDatas method run countSql:" + countSql);
		int start = 0;
		int limit = 0;
		if (inputMap.containsKey(ViewConstant.JSON_PAGE_START)) {
			String startStr = inputMap.get(ViewConstant.JSON_PAGE_START)
					.toString();
			if (ViewUtil.isNum(startStr)) {
				start = Integer.valueOf(startStr).intValue();
			}
		}
		if (inputMap.containsKey(ViewConstant.JSON_PAGE_LIMIT)) {
			String limitStr = inputMap.get(ViewConstant.JSON_PAGE_LIMIT)
					.toString();
			if (ViewUtil.isNum(limitStr)) {
				limit = Integer.valueOf(limitStr).intValue();
			}
		}

		viewGridDatasList = dicTableService.findData(tablecode, dicColumnDtos,
				sql, start, limit);
		return viewGridDatasList;
	}

	/**
	 * Description : 获取导出excle数据信息
	 * 
	 * @param userid
	 * @param viewpanelId
	 * @param dataList
	 * @return
	 * @throws BusinessException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List getParseExcelDatasFromList(long userid, String eventIdStr,
			List dataList) throws BusinessException {
		logger.info("getParseExcelDatasFromList method run begin.");

		Long viewEventId = ViewUtil.getViewControlId(eventIdStr);

		ViewEvent viewEvent = viewEventDaoImpl.get(viewEventId);
		if (null == viewEvent) {
			logger.error("配置出错,事件配置不存在! viewevent id:" + eventIdStr);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		Long viewWinID = viewEvent.getViewwinid();// 获取winid

		/* 获取winid对应的所有panel */
		List<ViewPanel> viewpanels = viewPanelDaoImpl
				.findByViewwinid(viewWinID);

		ViewPanel viewpanel = null;
		for (ViewPanel panel : viewpanels) {
			if (ViewPanel.TYPE_GRID.equals(panel.getType())) {
				viewpanel = panel;
				break;
			}
		}

		if (null == viewpanel) {
			logger.error("配置出错,页面面板配置不存在! viewpanel id:" + eventIdStr);
			throw new BusinessException("view.config.panel.noexists.exception");
		}

		String tablecode = viewpanel.getTablecode();// 获取表格内容对应的表

		List<Map> newDatasList = new ArrayList<Map>();
		LinkedMap titleMap = new LinkedMap();

		Long viewpanelId = viewpanel.getId();
		List<ViewItem> viewItemsList = viewItemDaoImpl
				.findItemByViewPanelid(viewpanelId);

		/* 添加标题 */
		for (ViewItem viewItem : viewItemsList) {
			titleMap.put(viewItem.getColumncodes(), viewItem.getFieldLabel());
		}
		/* 添加标题到列表中 */
		newDatasList.add(titleMap);

		/* 将记录集转化成map添加到列表中 */
		if (null != dataList && dataList.size() > 0) {
			this.parseRecordDatasToList(dataList, viewItemsList, tablecode,
					newDatasList, false);
		}
		logger.info("getParseExcelDatasFromList method run end.");
		return newDatasList;
	}

	private void parseRecordDatasToList(List dataList,
			List<ViewItem> viewItemsList, String tablecode, List newDatasList,
			boolean isShowHidden) throws BusinessException {
		// 提高性能,方法内缓存
		Map dataMapCache = new HashMap();
		Map<Long, DicColumn> finalDicColumnMapCache = new HashMap<Long, DicColumn>();
		for (int i = 0; i < dataList.size(); i++) {
			Object object = dataList.get(i);
			Map record = new LinkedMap();
			Map beanMap = null;
			if (object instanceof Map) {
				beanMap = (Map) object;
			} else {
				beanMap = new BeanMap(object);
			}
			for (ViewItem viewItem : viewItemsList) {

				String firstColumncode = viewItem.getFirstColumncode();
				if (viewItem.isMultilevelByName()) {
					// 要显示的值为关系表的数值

					// 下面处理分为两种情况，
					// 1、在查询结果中包含要显示的值，直接转换显示
					List<String> columncodes = viewItem.getColumncodes();
					DicColumn finalDicColumn = null;
					if (finalDicColumnMapCache.containsKey(viewItem.getId())) {
						finalDicColumn = finalDicColumnMapCache.get(viewItem
								.getId());
					} else {
						finalDicColumn = dicTableService.getFinalDicColumn(
								tablecode, columncodes);
						finalDicColumnMapCache.put(viewItem.getId(),
								finalDicColumn);
					}

					String attributecode = getColumnCodeByItemNameToJson(viewItem);
					boolean needGetEnumerateValue = false;
					String lastColumncode = columncodes
							.get(columncodes.size() - 1);
					String finalColumncode = finalDicColumn.getDicColumnPK()
							.getColumncode();
					if (finalDicColumn.getEnumerate().size() > 0
							&& !lastColumncode.equals(finalColumncode)) {
						attributecode = getAttributecode(columncodes,
								finalDicColumn);
						needGetEnumerateValue = true;
					}
					String name = getColumnCodeByItemNameToJson(viewItem);
					Object value = null;

					if (beanMap.containsKey(attributecode)) {
						if (null != beanMap.get(attributecode)) {
							String beanValue = beanMap.get(attributecode)
									.toString();
							if (needGetEnumerateValue) {
								value = finalDicColumn
										.getEnumerateValue(beanValue);
							} else {
								value = beanValue;
							}
						} else {
							value = "";
						}
					} else {
						// 2、不包含时，查询数据库得到结构后显示
						String beanValue = "";
						if (null != beanMap.get(firstColumncode)) {
							beanValue = beanMap.get(firstColumncode).toString();
						}
						String cacheKey = name + "=" + beanValue;

						if (dataMapCache.containsKey(cacheKey)) {
							value = dataMapCache.get(cacheKey);
						} else {
							value = dicTableService
									.findValueByTableCodeAndColumncodes(
											tablecode, columncodes, beanMap);
							dataMapCache.put(cacheKey, value);
						}
					}
					/* 如果是要添加隐藏烂尾 */
					if (isShowHidden) {
						record.put(name, value);
					} else if (!viewItem.getHidden()) {
						record.put(name, value);
					}
				} else {
					// 要显示的值为本表的数值
					if (null != beanMap.get(firstColumncode)) {
						record.put(firstColumncode, beanMap
								.get(firstColumncode).toString());
					} else {
						record.put(firstColumncode, "");
					}
				}
			}
			newDatasList.add(record);
		}
	}

	/**
	 * Description :上下移动排序
	 * 
	 * @param gridPanelId
	 * @param acitonType
	 * @param data
	 * @return 操作成功提示信息
	 * @throws Exception
	 */
	public Map Storting(String viewEventIdStr, JSONObject data)
			throws BusinessException {

		Long viewEventId = ViewUtil.getViewControlId(viewEventIdStr);
		ViewEvent viewEvent = viewEventDaoImpl.get(viewEventId);
		if (null == viewEvent) {
			logger.error("配置出错,事件配置不存在! viewevent id:" + viewEventIdStr);
			throw new BusinessException("view.config.panel.noexists.exception");
		}
		String actionType = viewEvent.getActionType();
		Long viewPanelId = viewEvent.getViewpanelid();

		ViewPanel gridPanel = viewPanelDaoImpl.get(viewPanelId);
		DicTable sourceDicTable = dicTableService.getDicTable(gridPanel
				.getTablecode());
		String sortingColumn = "";
		List<ViewItem> viewItemsList = viewItemDaoImpl
				.findItemByViewPanelid(viewPanelId);
		for (ViewItem viewItem : viewItemsList) {
			boolean orderFlag = viewItem.getOrderFlag();
			if (orderFlag == true) {
				sortingColumn = viewItem.getName();
				if (sortingColumn.contains(",")) {
					sortingColumn = sortingColumn.substring(0, sortingColumn
							.indexOf(","));
				}
			}
		}
		if ("".equals(sortingColumn)) {
			logger.error("配置出错,排序字段配置不存在! viewevent id:" + viewEventIdStr);
			throw new BusinessException(
					"view.config.panel.orderColumn.noexists.exception");
		}
		List<String> keyColumnList = sourceDicTable.getKeycolumnlist();
		List<Map> list = this.getSortingList(viewPanelId, actionType, data,
				sortingColumn);
		Map result = new HashMap();

		if ("rowTop".equals(actionType)) {
			// 选中的单条记录
			List<Map> checkedList = this.getSortingRecord(viewPanelId,
					actionType, data, keyColumnList);
			// 排序字段在页面上是desc排列的，所以最上面的记录排序值最大
			int maxSortingNumber = Integer.parseInt(list.get(0).get(
					sortingColumn).toString());
			int checkListSortingNumber = Integer.parseInt(checkedList.get(0)
					.get(sortingColumn).toString());
			if (maxSortingNumber == checkListSortingNumber) {
				result.put(ViewConstant.JSON_DEAL_FAILURE, true);
				result.put(ViewConstant.JSON_DEAL_MSG, "已经是第一行");
			} else {
				checkedList.get(0).put(sortingColumn, maxSortingNumber + 1);
				result = this.updateSortingDatas(viewPanelId, "edit",
						checkedList);
			}
		} else if ("rowBottom".equals(actionType)) {
			// 选中的单条记录
			List<Map> checkedList = this.getSortingRecord(viewPanelId,
					actionType, data, keyColumnList);
			// 排序字段在页面上是desc排列的，所以最下面的记录排序值最小
			int minSortingNumber = Integer.parseInt(list.get(0).get(
					sortingColumn).toString());
			int checkListSortingNumber = Integer.parseInt(checkedList.get(0)
					.get(sortingColumn).toString());
			if (minSortingNumber == checkListSortingNumber) {
				result.put(ViewConstant.JSON_DEAL_FAILURE, true);
				result.put(ViewConstant.JSON_DEAL_MSG, "已经是最后一行");
			} else {
				checkedList.get(0).put(sortingColumn, minSortingNumber - 1);
				result = this.updateSortingDatas(viewPanelId, "edit",
						checkedList);
			}
		} else if ("rowUp".equals(actionType)) {
			if (list.size() == 1) {
				result.put(ViewConstant.JSON_DEAL_FAILURE, true);
				result.put(ViewConstant.JSON_DEAL_MSG, "已经是第一行");
			} else {
				for (int i = 0; i < list.size() - 1; i++) {
					int temp = Integer.parseInt(list.get(i).get(sortingColumn)
							.toString());
					list.get(i).put(sortingColumn,
							list.get(i + 1).get(sortingColumn));
					list.get(i + 1).put(sortingColumn, temp);
				}
				result = this.updateSortingDatas(viewPanelId, "edit", list);
			}

		} else if ("rowDown".equals(actionType)) {
			if (list.size() == 1) {
				result.put(ViewConstant.JSON_DEAL_FAILURE, true);
				result.put(ViewConstant.JSON_DEAL_MSG, "已经是最后一行");
			} else {
				for (int i = 0; i < list.size() - 1; i++) {
					int temp = Integer.parseInt(list.get(i).get(sortingColumn)
							.toString());
					list.get(i).put(sortingColumn,
							list.get(i + 1).get(sortingColumn));
					list.get(i + 1).put(sortingColumn, temp);
				}
				result = this.updateSortingDatas(viewPanelId, "edit", list);
			}
		}
		return result;
	}

	/**
	 * 获取排序列表
	 * 
	 * @param viewpanelId
	 * @param acitonType
	 * @param data
	 * @param sortingColumn
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getSortingList(Long viewpanelId, String acitonType,
			JSONObject data, String sortingColumn) throws BusinessException {
		JSONObject baseParams = JSONObject.fromObject(data.get("baseParams"));
		JSONObject selectObj = JSONObject.fromObject(data.get("selectObj"));
		String sortingValue = selectObj.getString(sortingColumn);
		String compareSQL = "";
		String conditionSQL = "";
		String orderbySQL = "";
		int sortingStart = 0;
		int SortingLimit = 1;

		if ("rowUp".equals(acitonType)) {
			compareSQL = " >= ";
			orderbySQL = " order by " + sortingColumn + " asc ";
			sortingStart = 0;
			SortingLimit = 2;
			conditionSQL = " where " + sortingColumn + compareSQL
					+ sortingValue;
		} else if ("rowDown".equals(acitonType)) {
			compareSQL = " <= ";
			orderbySQL = " order by " + sortingColumn + " desc ";
			sortingStart = 0;
			SortingLimit = 2;
			conditionSQL = " where " + sortingColumn + compareSQL
					+ sortingValue;
		} else if ("rowTop".equals(acitonType)) {
			orderbySQL = " order by " + sortingColumn + " desc ";
			sortingStart = 0;
			SortingLimit = 1;
		} else if ("rowBottom".equals(acitonType)) {
			orderbySQL = " order by " + sortingColumn + " asc ";
			sortingStart = 0;
			SortingLimit = 1;
		}
		ViewPanel gridPanel = viewPanelDaoImpl.get(viewpanelId);
		String targetSQL = SQLConstructServie.ConstructTargetSQL(gridPanel);
		String sql = targetSQL + conditionSQL + orderbySQL;
		List<DicColumnDto> dicColumnDtos = findDicColumnsByPanelId(viewpanelId,
				gridPanel.getTablecode());

		List<Map> viewGridDatasList = new ArrayList<Map>();
		if (null != dicColumnDtos && dicColumnDtos.size() > 0) {
			Map baseParamsMap = ViewUtil.Object2Map(baseParams);
			String viewsearchidStr = "";
			boolean useCache = false;

			viewGridDatasList = this.getSortingDatas(gridPanel, viewpanelId,
					viewsearchidStr, dicColumnDtos, baseParamsMap, sql,
					useCache, sortingStart, SortingLimit);
		}

		return viewGridDatasList;
	}

	/**
	 * 获取被选中行的记录
	 * 
	 * @param viewpanelId
	 * @param acitonType
	 * @param data
	 * @param keyColumnList
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getSortingRecord(Long viewpanelId, String acitonType,
			JSONObject data, List<String> keyColumnList)
			throws BusinessException {
		JSONObject baseParams = JSONObject.fromObject(data.get("baseParams"));
		JSONObject selectObj = JSONObject.fromObject(data.get("selectObj"));
		int sortingStart = 0;
		int SortingLimit = 1;

		ViewPanel gridPanel = viewPanelDaoImpl.get(viewpanelId);
		String targetSQL = SQLConstructServie.ConstructTargetSQL(gridPanel);
		StringBuffer conditionSQL = new StringBuffer(" where ");
		if (keyColumnList.size() == 0) {
			logger.error("选择的记录中没有包含主键值.");
			throw new BusinessException("data.input.exception");
		}
		for (int i = 0; i < keyColumnList.size(); i++) {
			String keyColumn = keyColumnList.get(i);
			if (i == 0) {
				if (!"".equals(selectObj.getString(keyColumn))
						&& null != selectObj.getString(keyColumn)) {
					conditionSQL.append(keyColumn).append(" = ").append(
							selectObj.getString(keyColumn));
				}
			} else {
				if (!"".equals(selectObj.getString(keyColumn))
						&& null != selectObj.getString(keyColumn)) {
					conditionSQL.append(keyColumn).append(" and = ").append(
							selectObj.getString(keyColumn));
				}
			}
		}
		String sql = targetSQL + conditionSQL;

		List<DicColumnDto> dicColumnDtos = findDicColumnsByPanelId(viewpanelId,
				gridPanel.getTablecode());
		List<Map> viewGridDatasList = new ArrayList<Map>();
		if (null != dicColumnDtos && dicColumnDtos.size() > 0) {
			Map baseParamsMap = ViewUtil.Object2Map(baseParams);
			String viewsearchidStr = "";
			boolean useCache = false;

			viewGridDatasList = this.getSortingDatas(gridPanel, viewpanelId,
					viewsearchidStr, dicColumnDtos, baseParamsMap, sql,
					useCache, sortingStart, SortingLimit);
		}

		return viewGridDatasList;
	}

	/**
	 * 获取要排序的记录列表
	 * 
	 * @param gridPanel
	 * @param viewpanelId
	 * @param viewsearchidStr
	 * @param dicColumnDtos
	 * @param inputMap
	 * @param sql
	 * @param useCache
	 * @param sortingStart
	 * @param SortingLimit
	 * @return
	 * @throws BusinessException
	 */
	private List<Map> getSortingDatas(ViewPanel gridPanel, Long viewpanelId,
			String viewsearchidStr, List<DicColumnDto> dicColumnDtos,
			Map inputMap, String sql, boolean useCache, int sortingStart,
			int SortingLimit) throws BusinessException {
		String tablecode = gridPanel.getTablecode();// 获取表格内容对应的表
		List<Map> viewGridDatasList = null;
		viewGridDatasList = dicTableService.findData(tablecode, dicColumnDtos,
				sql, sortingStart, SortingLimit);
		return viewGridDatasList;
	}

	/**
	 * 保存修改排序值后的记录
	 * 
	 * @param gridPanelId
	 * @param acitonType
	 * @param inputMaps
	 * @return
	 * @throws BusinessException
	 */
	private Map updateSortingDatas(Long gridPanelId, String acitonType,
			List<Map> inputMaps) throws BusinessException {
		ViewPanel viewPanel = viewPanelDaoImpl.get(gridPanelId);
		BusinessDealFlowContext context = new BusinessDealFlowContext(viewPanel
				.getTablecode(), acitonType, inputMaps);
		businessDealFlowEngineService.run(context);
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		result.put(ViewConstant.JSON_DEAL_MSG, "操作成功");
		logger.info("saveOrUpdateDataInGridPanel method run end.");
		return result;
	}

}
